Scaling Node.js Real-time Apps with Windows Azure Service Bus

In this post, I will demonstrate how to scale out real-time Socket.IO apps with Windows Azure Service Bus by leveraging the Node.js module Socket.IO-ServiceBus. The Node module Socket.IO-ServiceBus lets the Node.js developers scale out the Socket.IO apps to multiple servers, which will work with Socket.IO store, provided by Windows Azure Service Bus Topic. The only Windows Azure component you need to scale out your Socket.IO apps with Socket.IO-ServiceBus, is Windows Azure Service Bus Topic. You can deploy your Socket.IO apps into any cloud platform or on-premise servers and, the apps deployed in multiples servers, that will be work through the Windows Azure Service Bus. This blog post assumes that you have already worked with Socket.IO apps and will be focused on how to scale out Socket.IO apps with Windows Azure Service Bus.

Socket.IO for Real-time Web Apps

Socket.IO is one of the most popular Node.js modules, which is widely used for building real-time web apps on the Node.js platform. Socket.IO provides cross-browser real-time communication to web browsers which  enables bi-directional communication between the server and browser client. Socket.IO leverages HTML 5 WebSocket if the browsers are support it, otherwise, it will use some browser-specific tricks to simulate the behavior provided by WebSockets, regardless of browser versions. Socket.IO can be used for building real-time apps such as Web Chats, Real-time Games, Stock monitoring apps, Push Notification Hubs, Real-time APIs, etc.

Scale Out Socket.IO Apps to multiple Servers with Socket.IO-ServiceBus

Node.js is a single threaded, asynchronous programming model, which enables a non-blocking execution models for developing web apps and networking apps. By default, for Node.js apps, you can’t leverage multi core machines, since Node.js is based on single threaded execution model, which will be leveraged single core of the server machine. But you can leverage multi cores of the server machine by leveraging the Node.js Cluster module. The problem with Socket.IO is that it would be difficult to scale out the Socket.IO server to multiple processes and multiple servers. Socket.IO provides a feature named Store which can be used for scale out Socket.IO apps to multiple processes. The Windows Azure Service Bus extends the Socket.IO store by providing a store option in Windows Azure, which provides greater scalability  for scaling the Socket.IO apps to multiple processes and multiple servers regardless of data centers used for the server apps. Socket.IO-ServiceBus is the module developed by Microsoft Windows Azure Team for scale out Socket.IO apps with the help of Windows Azure Service Bus Topics. The beauty of the Socket.IO-ServiceBus is that it is not necessary to deploy your Socket.IO apps in Windows Azure. You can deploy your Socket.IO apps in any cloud platform or your own on-premise server. The only essential component for working with Socket.IO-ServiceBus is Windows Azure Service Bus Topic. Socket.IO-ServiceBus will leverage the Windows Azure Service Bus Topic as the store option for Socket.IO server instances.

The following are the basic steps for configuring Socket.IO store with Windows Azure Service Bus using the Socket.IO-ServiceBus

Step 1 – Create Windows Azure Service Bus Namespace and Topic

In order to working with Socket.IO-ServiceBus, we need to create a Windows Azure Service Bus namespace and Topic to configuring the store for Socket.IO server. To create a Windows Azure Service Bus namespace, go to  Windows Azure management portal, select Service Bus from left pane, click Create from lower side of the windows and add a new namespace as shown in the following picture

image

You can copy the connection string of Service Bus from “Connect Info” button, after the choosing the namespace, which you want to copy the connection string.image

We have created a namespace in Windows Azure Service Bus. Let’s create a Windows Azure Service Bus Topic within the namespace we have created in the previous step, for configuring the Service Bus store for Socket.IO server apps. To create a Service Bus Topic, select New from the lower left corner, select App Services, Service Bus, , Topic and click Quick Create and give the Topic Name and choose region and Namespace.

image

Step 2 – Configure Windows Azure Service Bus Store for Socket.IO Servers

In the step 1, we have created Windows Azure Service Bus namespace and Topic. In this step, let’s configure the Windows Azure Service Bus store for Socket.IO. First, add the npm module socket.io-servicebus to your Socket.IO app.

image

The code block below configures the socket.io-servicebus module with Windows Azure Service Bus connection string and topic name.

var socketio = require('socket.io')
    ,SbStore = require('socket.io-servicebus');
 
 
var io = socketio.listen(server);
//Configure Service Bus Store
io.configure(function () {
  io.set('store', new SbStore({
    topic: topicName,
    connectionString: sbconn
  }));
});

In above code block, the variable name topicName contains the name of Windows Azure Service Bus Topic and the variable name sbconn contains the connection string for Windows Azure Service Bus. You can deploy the Socket.IO apps to any number of servers and the only piece of code you need to do is configure the socket.io-servicebus module with above code block.

Summary 

The npm module socket.io-servicebus, developed by Windows Azure team, is an excellent framework which lets the Node.js developers to build highly scalable real-time Node.js apps by simply leveraging Windows Azure Service Bus. The socket.io-servicebus does not restrict you to deploying your Node.js server apps only with Windows Azure. You can deploy your Socket.IO apps to any kind of servers and the only Windows Azure component you required is the Windows Azure Service Bus subscription. The socket.io-servicebus  is an open source framework that available in github from here. I would greatly appreciate the Windows Azure team for their openness and contributions to OSS systems. I hope that socket.io-servicebus will enable better performance in future releases which will enable us to building mission critical real-time Node.js apps with Windows Azure Service Bus.

You can follow me on twitter at @shijucv

No Comments