In this article we will be building a socket.io and NodeJs Chat application
We will be building a pretty simple application that you can easily follow along and all the code will be available on github to easily download and run.
All the code is free and opensource
In this tutorial we will be building the app with basic HTML CSS along with Socket.io and NodeJS.
New to DeadSimpleChat? It's a turn key chat that you can easily add to your website or App —without any complicated code. For Virtual / Live events, SaaS App, Social Platform, Education, Gaming, Finance Sign Up for Free
Let us look at how the finished application with HTML and CSS front end will look like:
Step 1: Scaffolding a new project for chat with NodeJs
let us first start building the backend. Type the npm init command to initialize a new project.
let us name the project socketio-nodejs-chat-application
now let us install socket.io by typing the below command
npm install socket.io --save
and install expressjs as well
Open the code in your favorite code editor I am using VSCode. The code should look like this
The package.json should look something like:
Now create a new file called index.js and paste the below code in it.
What are we doing here
We have initialized the express and we are sending Hello world whenever a request comes to the server
type node index.js to start the server
Our express server is listening at port 3000 . So if we go to the localhost:3000 we can see the Hello World there like this:
Step 2 : Creating font-end for Chat with HTML and Socket.io
Let us now create a new file called index.html We will render this file using express.js
Paste the below code in the index.html file
In this index.html file we are creating a chat layout with a form and input for writing text and a button to send the message
Plus list to display messages and highlighting that with CSS
Now edit the index.js file to send the index.html file instead of sending Hello World
We use the method res.sendfile to send a file.
The index.js should look like this
Now if you go to localhost:3000 you will the simple chat layout that we have created using html and CSS
Step 3: Adding Socket.io functionality
Now let us add the socket.io functionality to send messages. Paste the below code in index.js file
We initialize the instance of socket by passing the http server object to it.
Then we listen to the connection event for the incoming sockets and log it to the console
lets run it, restart the server by pressing ctrl +c and again typing node index.js
the server crashes, if we look into the console we see CORS error. let's fix this
In the index.js file type delete the code const io =newServer(server);
and type the code
Now restart the server again. The index.js file should look like:
and open a new window and go to the localhost:3000 you will the following message in the console
this is the server part, now let us add the socket.io in the HTML
Add the below script tags in the index.html file
The index.html file should look like this
Now you have connected the html with the NodeJs backend. The socket.io also fires a disconnect event whenever a disconnect event occurs
Add the below code in your index.js file to console.log when a user disconnects.
now restart the server and reload the chat webpage you can see a user connected and ` user disconnected` in the console.log
Next we need to emit events, socket.io supports any data that can be encoded in JSON (binary data is supported too).
So along with the events we can send the message that we want to broadcast to all the users connected to the chat
So, let us send the messages to the server as the user types them. For that type the below in the index.html file
and in our server we will receive the message and emit it or broadcast it to all the users connected to the chat
Step 4: Sending and receiving messages
In our chat example we will be sending the chat messages to every user that is connected to the chat, even the user that is sending the message
There is an io.broadcast.emit method if you want to send the message to everyone except the user who sent the message
Paste the below code in the index.js file
The total index.js code looks like
On the index.html page we will capture the message sent from the server and show it on the chat webpage
In the index.html add the following code below the initalization code.
The complete code for the chat looks like
index.html
and index.js
and lastly package.json
Our completed chat application looks like:
Using Pre-Built Chat
Making your own scale and reliable chat solution is a massive endeavour.
Instead you can choose DeadSimpleChat.com A pre-built chat for any use-case
Group Chat
Live Streaming Chat
1-1 Chat
Chat API, SDK and Webhooks
DeadSimpleChat is a no code chat. you can easily add it to your website or app and customize it with the UI based customization.
But, there are APIs, a JavaScript SDK and Webhooks if you need precision customization and integration with your website and app
let us visit DeadSimpleChat and go through some of its features and functionality
you can create a free account by clicking on the Get Started for Free button
Once you have created an account you will land up on the Dashboard page
Here you can see Chat Analytics and Create a new Chat room by clicking on the create chat room button
You can also see a list of chat rooms that you have created by click the chat rooms button in the side panel
When you click on the create chat room button. It asks you some basic questions like the name of the chat room, its description etc and then a chat room is created and you land on the General settings page of the chat room
Here you can enable disable chat room features according to your needs
There are a variety of settings here that you can customize the chat with
For example
you can turn on/turn off the chat
2. Ability to like/react to messages
3. Q&A mode
4. Sub Channels
5. file and  image sharing
6. 1-1 Chat
7. Password Protect chat
8. Enable disable chat notifications
9. Private room
10. Eject All participants
12. Export all messages and files
13. Delete Chat rooms
Once you have customized the chat room setting you can customize the look of the chat room by going to the customize section
DeadSimpleChat has a UI based chat customization tool. You can customize the look of the chat exactly as you want to
You can even write Custom CSS to precision customize the look of the chat room
You can set the size of the chat room, copy customizations to other chat rooms and much more
All the chat rooms in DeadSimpleChat are completely independent of other chat rooms.
At the bottom you can see the embed code. Just paste the embed code on your website or app where you want to add the chat room and the chat is added on your website or app
Then you can create Channels/ sub rooms when are chat rooms inside of chat rooms
next to that is the translate tab. You can translate Dead Simple Chat UI into any language you want.
After that you can go to the embed page there you can see the embed info
You can also change the size of the chat room and add it as a chat bubble as well
Apart from this you can create moderators. Moderators can delete messages, ban users.
You can also ban bad words from the chat room. You can use our pre defined list or create custom bad words as well.
There are also API, Chat SDK and Webhooks as well.