[NodeJS] Realtime Chat with Node.js
In this tutorial, we are going to build a realtime chat system with Node.js and the socket.io library. The chat permits users to create private chat rooms that they can share with a friend. For avatars, we will use gravatar. You can run the chat locally with node, or push it to heroku or a different cloud service provider.
You can grab the source code from the download button above. It has plenty of comments and is easy to follow. Start with the app.js file and read from there. Here are a few things to look for:
- All dependencies are declared in the package.json file. They are not included in the zip and you will have to run
npm install to get them.
- In the routes file, socket.io stores the username, avatar and room of the person as properties of the socket object itself. This saves us a lot of code and makes managing rooms easy.
- We use socket.io‘s rooms feature to isolate one chat from another, which is exactly what that feature was developed for.
Enter your name to chat