Creating the Chat Server Functionality

With your WebSockets server up and running, you are now ready to build out your chat server. Your chat server needs to do a few things:

  • keep a log of the messages sent so far to the server

  • broadcast older messages to new people joining the chat

  • broadcast new messages to all clients

Keeping a log of messages as your users send them is necessary in order to send the message history to new users, so you will tackle that first.

In websockets-server.js, create an array to hold on to messages.

var WebSocket = require('ws');
var WebSocketServer = WebSocket.Server;
var port = 3001;
var ws = new WebSocketServer({
    port: port
});
var messages = [];

console.log('websockets server started');
...

If you were creating a more robust chat system, you might store your messages in a database. For now, a simple array is fine.

Next, call messages.push(data) to add each new message to your array as it arrives.

...
ws.on('connection', function (socket) {
  console.log('client connection established');

  socket.on('message', function (data) {
    console.log('message received: ' + data);
    messages.push(data);
    socket.send(data);
  });
});

Just like that, you have an array of all the messages that have been received by your chat server.

The next step is to allow new users to see all the previous messages. Update the connection event handler in websockets-server.js to send out all the old messages to each new connection as it arrives.

...
ws.on('connection', function (socket) {
  console.log('client connection established');

  messages.forEach(function (msg) {
    socket.send(msg);
  });

  socket.on('message', function (data) {
    console.log('message received: ' + data);
    messages.push(data);
    socket.send(data);
  });
});

As soon as a connection is made, the server iterates through the messages and sends each one to the new connection.

The last job is to send new messages to all the users as each new message comes in. WebSockets keeps track of your connected users for you. Use this mechanism in websockets-server.js to rebroadcast your received messages.

...
ws.on('connection', function (socket) {
  console.log('client connection established');

  messages.forEach(function (msg) {
    socket.send(msg);
  });

  socket.on('message', function (data) {
    console.log('message received: ' + data);
    messages.push(data);
    ws.clients.forEach(function (clientSocket) {
      clientSocket.send(data)
    });

    socket.send(data);
  });
});

The ws object keeps track of all connections via its clients property. It is an array that you can iterate through. In your iterator callback, you only need to send the message data.

Finally, because you end up sending your message to your own socket when you iterate over all the sockets, you no longer need the call to socket.send(data). Deleting it cleans things up nicely.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.159.223