Chat on the terminal
Chat on the terminal
Build a realtime chat in your terminal 💬
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 message
s 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;
chat();
});
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}`);
chat();
});
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);
chat();
});
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 👨💻
https://repl.it/@jajoosam/chat
Now, go ahead and share what you've built on repl talk, and tag me - @jajoosam 👤
it says cannot GET /
cool
@jajoosam How would we make this an actual chat website? repl.run is broken :(
I really do appreciate this, thanks. got me started with express and all.
This is great! Would you like to work on a secret project with Battledash, RYANTADIPARTH, and I?
hello
cool! and it’s so sad that repl.run doesn’t work anymore :(
Very creative!
/skin /tanz
Repl.run is no more :(
Hallo
I wish I could build one
is there a way to delete all the code on top?
Nice one, but it says 'io' is not defined...
well u need to add socket.io. import socket.io and set that to a veriable, maybe "socket". io is then equal to socket(server) @DantheFan
How would you make a different server?
I didn't work
@LyleOlivarez You have to go to https://chat.jajoosam.repl.run/
Nice
I can't connect to the website
danktastic
use the node tcp server(NET MODULE), would do a tutorial on that very soon.
https://nodejs.org/api/net.html
I want to make a chatting website, how do you do that?
at the 'setting up the server' bit, I get invalid syntax at the end of the first line.
WOW!
that's so cool
they should use your awesome abilities to do something great in the world!
:)
sad