repl.it
Nodejs

Chat on the terminal with repl.run!

fork
loading
Files
  • index.js
  • package-lock.json
  • package.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var io = require('socket.io-client');
const chalk = require('chalk').constructor({ enabled: true, level: 3 });;

var socket = io("https://chat-server--jajoosam.repl.co");

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});


var id = "";
var buffer = "";


function chat(){
	rl.question(chalk.magenta("» "), (answer) => {
		buffer = `${chalk.cyan(id)} : ${chalk.bgCyan(answer)}`;
		socket.emit("message", buffer);
		chat();
	});
}

socket.on('connect', () => {

	rl.question(`What's your name? `, (answer) => {
			socket.emit("message", `👤  : ${chalk.green(answer)} has joined the chat`);
			id = answer;
			chat();

	});

	socket.on('msg', function(data){
		if(buffer!=data){
			console.log("\n" + data);
			chat();
		}
	});
	
})
node v10.16.0