Chapter 13. Building an Advanced Chat App

In the previous chapter, we developed a rudimentary chat application, which allowed an arbitrary number of users to connect to each other and talk anonymously.

In this chapter, we are going to expand this app and make it more advanced by adding features for chat rooms and notifications. In doing so, we will demonstrate how the concept of namespacing works on socket.io, which is one of the most important aspects of this library.

We need some room!

So far, the most advanced thing that we have done with WebSockets in our apps has simply been sending data back and forth across a single WebSocket interface. We paid very little attention to partitioning and basically just let it all go on as a free-for-all app. However, in real life, we will frequently find ourselves in situations where we want to partition WebSocket connections and only let certain users have access to a subset of partitions.

To see how this can work, consider the case of a group chat. Here, rather than having just a single solitary chat interface, users instead have access to a multitude of them; each hosts its own members and conversation. To implement this, we can extend our existing chat server to simply start new node instances for the chat rooms that we want to open, with each of them having its own port, as follows:

// [snip]

// Connect the websocket handler to our server
var websocket = require('socket.io')(server);
// Create a handler for incoming websocket connections
websocket.on('UserConnectedEvent', function (socket) {
  console.log("New user connected");
  // Tell others a new user connected
  socket.broadcast.emit('UserConnectedEvent', null);
  // Bind event handler for incoming messages
  socket.on('MessageSentEvent', function (chatData) {
    console.log('Received new chat message');
    // By using the 'broadcast' connector, we will
    // send the message to everyone except the sender.
    socket.broadcast.emit('MessageReceivedEvent', chatData);
  });
});

// Define a separate port for each server we want to start
var port = 8080; // get from terminal args, for example

server.listen(port);

However, this becomes clunky very quickly. Since we will need to fire up a new, separate V8 instance for each server, chances are that we will very soon get angry knocks at the office window from the guy down the hall whose super-important stock analysis algorithm just crashed due to a lack of memory space. He may want to hurt us and do terrible things to our pets, all because we could not find a smoother way to make use of WebSockets.

Or, well, maybe we can, after all.

This is where the concept of a namespace comes into play. Imagine a situation where we can just partition a single socket.io instance into several different endpoints, each of which can service its own set of clients. It turns out that we can!

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

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