Learn to Code via Tutorials on Repl.it!

← Back to all posts
zplusfour (890)

Hi, this is a tutorial about how to build a chat app

here is it @Leroy01010

Let's start

1. Libraries

We need socket.io to make a connection from user to a user.
And we need an express server, and also http.


const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);

2. Socket.io connection

We need some lines of code, to make a websocket connection.

The first block of code is for the username (for the chat app) and then to send: 'Username joined the chat...'

The second block of code is the opposite of the first one.

The third block of code is about the message, when the user sends a message, it will send his message with the username and the date of the message.

The fourth code block (outside of the websocket connection) is the port listen handler.

io.sockets.on('connection', (socket) => {
  socket.on('username', function(username) {
    socket.username = username;
    io.emit('is_online', '🔵 <i>' + socket.username + ' joined the chat..</i>');

  socket.on('disconnect', (username) => {
    io.emit('is_online', '🔴 <i>' + socket.username + ' lefted the chat..</i>');

  socket.on('chat_message', (message) => {
    let time = new Date();
    io.emit('chat_message',`<i>${time.toLocaleTimeString()}</i><br>` +  '<strong>' + socket.username + '</strong>: ' + message);

// Server listen handler
server.listen(8080, ()=>{

4. Render the HTML file for the chat.

We need to add some lines before the socket connection.
To render the HTML chat app file.


app.get('/', function(req, res) {
  res.sendFile(__dirname + "/index.html");

5. Fill the index.html with code to make a chat app.

We need to use jQuery to make a form for the chat.


<!DOCTYPE html>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font: 13px Helvetica, Arial; }
        form { background: #fff; padding: 3px; position: fixed; bottom: 0; width: 100%; border-color: #000; border-top-style: solid; border-top-width: 1px;}
        form input { border-style: solid; border-width: 1px; padding: 10px; width: 85%; margin-right: .5%; }
        form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; margin-left: 2%; }
        #messages { list-style-type: none; margin: 0; padding: 0; }
        #messages li { padding: 5px 10px; }
        #messages li:nth-child(odd) { background: #eee; }
    <script src="../../socket.io/socket.io.js"></script>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
  <body style="background: grey;">
      <header align="center">
    <ul id="messages"></ul>
    <form action="/" method="POST" id="chatForm">
      <input id="txt" autocomplete="off" autofocus="on" placeholder="Your message" /><button>Send</button>
            var socket = io.connect('https://chatorzo-2.zdev1.repl.co');
                socket.emit('chat_message', $('#txt').val());
                return false;
            socket.on('chat_message', function(msg){
            socket.on('is_online', function(username) {
            var username = prompt('Username:');
            socket.emit('username', username);

So we basically added some CSS and some javascript (jQuery) to:

  • Make it responsive and beautiful.
  • Connect the socket code to the HTML file.

And that's it

Have a great day!

Source code: https://repl.it/@ZDev1/chatorzo-2

Muhammad_SJC (149)

That is a great chat app. But I have found a tool called Google Firebase that actually handles all the backend database for you. Check out the chatting app I made with Firebase: https://repl.it/talk/share/Chat-App-Using-Firebase-Realtime-Database/55382

If you want a tutorial on how to make this, then please upvote!

zplusfour (890)

@Muhammad_SJC this is cool!
nice chat!
I know what is firebase, I'll learn it
but tell me, why did you make the username as a javascript prompt?
you can do a google auth with firebase!

Muhammad_SJC (149)

@ZDev1 I know and I am working on it, but this is just a prototype

moradz (1)

Well done zohdi
You are awesome

dzintarsb2 (0)

ay yo do you have discord , i have few question to ask , would really appriciate it my g!

zplusfour (890)

@dzintarsb2 i'm very sorry but i don't have discord
would you like to ask me on MS Teams lmao?

dzintarsb2 (0)

@zplusfour i don't have that too LMAO , do you have any other contacts that you use actively?

zplusfour (890)

@dzintarsb2 btw it's microsoft teams...
maybe slack?
or gmail XD
or we can just create a repl and chat on it

zplusfour (890)

@dzintarsb2 ah c'mon ima create a repl and invite u

nngggaming (0)

Kind of good and bad... good: it’s a wonderful chat app. Bad: it is connected through all repls? I followed all the steps above, and sent some messages, at the same time opened up another tab with your repl and yeah saw the messages i sent...

Is there any way to make it more private(like only people with my repl project link can see the messages)?

zplusfour (890)

@nngggaming I have used Socket.IO
which it let's you to connect users to other users.
It is not a database, messages aren't saved in the database, only if someone joined the repl (if one of your family or friends or people on repl.it) can see messages on this web.

M4urice (0)

This may be a stupid question but I'm rather new to coding and I'm unsure of where to place the code for rendering the HTMl

zplusfour (890)

@M4urice it is fine
first, just make a HTML file (index.html)
And this block of code, renders the html file:

app.get('/', function(req, res) {
  res.sendFile(__dirname + "/index.html");

And then you'll be ready :)

M4urice (0)

Thanks for the help @ZDev1

ColeBosman (12)

It says .io isnt defined.

ColeBosman (12)

@ZDev1 Do you know any potential fixes?

oignons (237)

It's XSS-vulnerable.

Highwayman (1441)

I feel...so lost.... :(

I can’t understand the html stuff very well without any explanations or breaking down.

zplusfour (890)

@Highwayman oh sorry
I'll explain to you:
in the HTML file, we did:

  • Styling to make it colorful (not a lot)
  • And then I wrote then socket.io cdn for the chat, and a jQuery cdn
  • And then this <ul> is the messages container
    When someone sends a message it will append in it as a <li> element
  • And the form is where you need to write your message
  • And the script tag, I wrote in it some code to:
  1. connect the websocket to the website
  2. make a little bit jQuery code to submit the form when you send a message
  3. and some append things (I explained in this comment)
    and then the username, it will be as a javascript prompt!

And that's it!

Highwayman (1441)

Wow thank you, very succinct! The ul thing is neat :)

retronbv (128)

lefted the chat.

left* the chat

Also when trying to send message

Cannot POST /

retronbv (128)

@ZDev1 Cannot POST / when trying to send msg

zplusfour (890)

@retronbv ok ok, I AM TRYING TO FIX THIS

retronbv (128)


Highwayman (1441)

Lol this convo is literally me and my best friend during any game.

ZenoxHosting (12)

ReferenceError: Server is not defined

any fix?

EpicGamer007 (1615)

Also, the repl in the link, it doesn't work:

EpicGamer007 (1615)

@CodeLongAndPros , im on school comp, i cant download extensions :(

AstOwOlfo (257)

How is it related to repl? That’s the default ejs error page. It’s actually @ZDev1 who is responsible for the light theme @CodeLongAndPros

zplusfour (890)

@agent9002 he means that he isn't using an extension that makes every website (error page is a website) has a dark theme

CodeLongAndPros (1593)

@ZDev1 No, the Console is light theme, which sucks.

EpicGamer007 (1615)

@ZDev1 , since when did md suck?

zplusfour (890)

@EpicGamer007 md sucks when you're trying to do ascii

EpicGamer007 (1615)

Wow! This is really cool! More node tuts plez! I definetly wanna try this.

zplusfour (890)

@Leroy01010 thanks!
Learn it and submit your mission to your teacher!