Simplest chat example in node.js and socket io

No Comments

Simplest Chat Server in Node.js implemented with

I’ll directly start since you guys don’t need introduction to node.js.I would like to comment on the feature exposed in this example of Nodejs and power of javascript running in both client and server side, it can use the event emitters  - event production and consumption design pattern elegantly.

I’ll give you some brief about is a clean library with simple api calls to make possible socket level communication in node.js. It uses websocket feature in html5 but gracefully falls back to other techniques with older browsers.

Methods of

  • socket.emit('message','the message to be transmitted'); //simple transmission
  • io.sockets.emit('message',"the message to be transmitted"); //send to all clients
  • socket.broadcast.emit('message',"this is a test"); //send to all except sender

Let’s start creating a single room minimal chat

In this we have the following use – cases

  1. User when join the chat room, he is greeted.
  2. All Users are notified who enters the chat room.
  3. User can broadcast the message to the chat room.

Create a node module chat_server.js to achieve the above objectives.

A static file server which listens on port 80.

Now the client code



Leave a Reply


Powered by Ajaxy