Build a simple chat room with Node.js, Socket.IO, and Express (part 2)

At first, please install Lodash and Validator packages by running the following command:
$ npm install lodash validator --save

Read more

The error message will be:

and the success message will be:
(Alice received the message when Clara joined because they are in the same room #mom)

To do this, we add an id to the form, a div tag to show the error message, change the button type to submit.

Add the following to index.html (before closing body tag):

<!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Join</title><link rel="stylesheet" href=""><link rel="stylesheet" href="/css/style.css"></head><body><div class="join-wrapper"><form class="form-horizontal" id="frm-join"><h2 class="text-center">Please Join</h2><div id="join-error-msg" class="alert alert-danger hidden" role="alert"></div><div class="form-group"> <label for="txt-display-name" class="col-sm-4 control-label">Display name</label><div class="col-sm-8"> <input type="text" required class="form-control" id="txt-display-name"></div></div><div class="form-group"> <label for="sl-room" class="col-sm-4 control-label">Room</label><div class="col-sm-8"> <select class="form-control" id="sl-room"><option value="mom">Mom</option><option value="kids">Kids</option><option value="sell_off">Sell off</option> </select></div></div><div class="form-group"><div class="col-sm-offset-4 col-sm-8"> <button type="submit" class="btn btn-primary">Join now</button></div></div></form></div> <script src=""></script> <script type="text/javascript" src="/"></script> <script>var socket=io();socket.on('newMessage',function(message){console.log(message);});var selectedRoom;$(function(){$('#frm-join').on('submit',function(e){e.preventDefault();selectedRoom=$('#sl-room option:selected').val();console.log('Selected room:',selectedRoom);socket.emit('join',{room:selectedRoom,display_name:$('#txt-display-name').val()},function(err){if(err){$('#join-error-msg').removeClass('hidden');$('#join-error-msg').text(err);}});});});</script> </body></html>

and to server.js

var express=require('express');const path=require('path');const http=require('http');const socketIO=require('');const _=require('lodash');const validator=require('validator');const port=process.env.PORT||3000;var app=express();app.use(express.static(path.join(__dirname,'./public')));var server=http.createServer(app);var io=socketIO(server);io.on('connection',(socket)=>{console.log('New user connected');socket.on('join',(params,callback)=>{var isRoomValid=_.isString(,['mom','kids','sell_off']);var isDisplaynameValid=_.isString(params.display_name)&&!validator.isEmpty(params.display_name.trim())&&validator.isAlpha(params.display_name)&&validator.isLength(params.display_name,{min:3,max:15});if(!isRoomValid){return callback('Please choose a room from dropdown list.');} if(!isDisplaynameValid){return callback('Please enter a valid display name (3-15 alphabetic characters).');} socket.join(;'newMessage',`${params.display_name}has joined.`);callback();});});server.listen(port);

In part 3, we will integrate with HTML to display like the following screenshot:

See you then!


  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer Node js Training in Chennai . learn from or Javascript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai

  2. Such a great blog and all the writings. Can’t help thinking about the author.
    Base Chat Number

  3. Every one of the substance you said in post is too great and can be exceptionally valuable. chatting

  4. Do you know how to make money fast? Come to our online casino site. great online slot games Don't live, but just win right now.

  5. your article is a very helpful for everyone. i have a also very good article for Technology , Food & Drink then check it..

  6. Actually I read it yesterday but I had some thoughts about it and today I wanted to read it again because it is very well written. chat

  7. This is my first time visit here. From the tons of comments on your articles,I guess I am not only one having all the enjoyment right here! hkeasychat