Learn to Code via Tutorials on Repl.it!

← Back to all posts
Chat on the terminal
jajoosam (896)

Chat on the terminal

Build a realtime chat in your terminal 💬

Demo ⏯️ Code 👨‍💻

This is a quick tutorial which helps you build a realtime chat on your terminal - using socket.io - we can build and test super quick thanks to repl.it's repl.run platform - which displays a terminal on the web 🌐 👨‍💻

⚙️ Dependencies

We're only using one external dependency in this project - socket.io, which helps us communicate realtime between several clients and a server.

Along with this, we'll be using node's built in readline which lets us collect input from the terminal.

💁 Setting up the server

Because we're using socket.io - we need to spin up a small server to relay messages too! Just fork this, and you'll be all set 👉 repl.it/@jajoosam/chat-server

io.on('connection', function (socket) {
	socket.on('message', function(data) {
		io.emit("msg", data);

This small block of code listens for any messages from any user, and then broadcasts that message to all the users - that simple ✨

💬 Let's get chatting

Let's start off by forking this repl - I've already declared dependencies to make things simple. On line 3, replace the url with that of the server you just created.

Whenever a user connects, let's ask them for their name. Put this code inside the socket.connect block.

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

We're sending a message which says a new user has joined the chat, and storing their name for later reference. As you can see - we're also calling the chat function, which is currently empty - let's change that!

rl.question("» ", (answer) => {
    // "» " Gives a prompt for the user
    socket.emit("message",  `${id}: ${answer}`);

We're prompting for a user to type out a message, then sending it back to the server - also at the end - we're calling chat again in case they want to send more messages ⌨️

🖨️ Printing out messages

If you try running this code now, you'll see that no message you type is printed in the terminal. Let's create a new event listener inside socket.connect to listen for messages 👇

socket.on('msg', function(data){
    console.log("\n" + data);

Whenever the server broadcasts a new message to us, we print it out to the console - and call chat again - so that our user can keep sending messages!

🛠️ Hacking this

It was pretty simple to build this chat - but it doesn't seem like a conventional one in many ways. Luckily, you can change that 😄

Here are some ideas to tinker with this project 👇

  • Let everyone know when a user is typing ⌨️
  • Don't print out messages sent by the same user repeatedly 🔄
  • Add color + formatting to the chat with chalk 🌈
  • Take realtime to the next level by broadcasting every character as it's typed 💡

Check out this open source project for more ideas!

Here's the code for the completed project (with a few tweaks!) - feel free to refer to it 👨‍💻


Now, go ahead and share what you've built on repl talk, and tag me - @jajoosam 👤

EpicGamer007 (1676)

Repl.run is no more :(

zaydi (10)

rip so sad ): neither is open in the new tab for python rip [email protected]

Paultheriault (18)

"Swastika" as a username? Isn't that toeing the line of political correctness a bit?

Lathryx (4)

@zacharymetz Look at the GIF at the top of the page. :/

tirthyme (0)

@Paultheriault it's a common name for Indians

Ridgeity (0)

@Paultheriault I think you're taking it a little too seriously, it was probably just the first word he thought of. he's not making a statement, is he? correct me if I'm wrong

InforMatheMathe (0)

@Ridgeity Quoting wikipedia:
"In many Western countries, the swastika is now viewed as a symbol of racial supremacism and intimidation because of its association with Nazism"

AliceInDystopia (1)

@InforMatheMathe key word, western, in most asian countries swastika is a peace symbol

firefish (931)

swastika is a peace symbol

@AliceInDystopia Ah, well actually the peace symbol is the back-to-front swastika

Nayoar (566)

Hey! Nice tutorial but a few issues:
Firstly, it doesn't work, in that messages you send aren't actually posted. Easy fix: replace

rl.question("» ", (answer) => {
    // "» " Gives a prompt for the user
    socket.emit("message", buffer);


rl.question("» ", (answer) => {
    // "» " Gives a prompt for the user
    socket.emit("message", answer); // <-- There - replace buffer with answer!

and it should work fine!

Also, if you want it to display who posted the message, change socket.emit("message", answer); to socket.emit("message", id+": "+answer);

Awesome tutorial though, I'm no good with JS so I would never have been able to create something like this without a tutorial!

My chatroom

jajoosam (896)

@minx28 Ah, fixed this - thanks for the spot :)

dugo3number3 (0)

@Nayoar You could also use the string (not sure what it's called in js but in python it's called an fstring)

KhalilNourElDin (3)

Really amazing!
But like one thing, if I'm typing and a person writes something it pushes my cursor all the way back, unsure why. I'm not the best with JS so yeahh
Please try to fix it because it's the last thing and it would be perfect!
Thanks :)

AnthonyMouse (81)

This is great! Would you like to work on a secret project with Battledash, RYANTADIPARTH, and I?

ProvingDev (1)

Hello, this tutorial was very useful for me, but I've got a problem...
I'm wondering how to make a mute command on this chat.
I would appreciate any help! :)

XanmanGaming (1)

all the chat-starter does is print some socket stuff in green help

codermaster8 (5)

Interesting it’s a very nice communication

nhlanhla1997 (0)

i too come to a school for code its me nhlanhla

nhlanhla1997 (0)

hi i need to about code someone can hepple me

ShouqMohammed2 (0)

Hi guys anyone can help me

ParrotDev (5)

@jajoosam How would we make this an actual chat website? repl.run is broken :(

frionx (51)

I really do appreciate this, thanks. got me started with express and all.

AnthonyMouse (81)

cool! and it’s so sad that repl.run doesn’t work anymore :(

dugo3number3 (0)

@AnthonyMouse at least spotlight exists