Socket.IO is a real-time application framework that allows for cross-browser, real-time communication between a browser and server.
The lack of browser and server support for the emerging WebSocket standard means we cannot easily achieve real-time communication across browsers. In order to achieve this, Socket.IO supports multiple transport protocols including WebSockets, long polling, XHR, and flashsockets, that function as a fallback mechanism for older browsers. Browsers that do not support WebSockets will simply fall back to a transport protocol they do support.
Socket.IO comes in two parts: a server-side module and a client-side script. Both parts need to be installed in order for our application to support bidirectional duplex communication. Let's install the server piece via NPM:
npm install socket.io --save
Let's configure our application to use Socket.IO by updating our ./config/*.json
config files with the following configuration:
"sockets": { "loglevel": 3 , "pollingduration": 10 , "browserclientminification" : false , "browserclientetag" : false , "browserclientgzip" : false }
The next step is to wire up Socket.IO to Express. Let's create and configure a typical Socket.IO server: ./lib/socket/index.js
. We define our Socket
module, which accepts a single argument: server
. We require the socket.io
module and create a new Socket.IO server, passing our Express-enabled HTTP server to it. We then configure our Socket.IO server by setting sensible values for log level
, transports
, and polling duration
, as defined previously in our config files, and return the Socket.IO server.
var config = require('../configuration'), function Socket(server) { var socketio = require('socket.io').listen(server); if (config.get('sockets:browserclientminification')) socketio.enable('browser client minification'), if (config.get('sockets:browserclientetag')) socketio.enable('browser client etag'), if (config.get('sockets:browserclientgzip')) socketio.enable('browser client gzip'), socketio.set("polling duration", config.get('sockets:pollingduration')); socketio.set('log level', config.get('sockets:loglevel')); socketio.set('transports', [ 'websocket' , 'flashsocket' , 'htmlfile' , 'xhr-polling' , 'jsonp-polling' ]); return socketio; }; module.exports = Socket;
Setting log level
is useful for debugging. Socket.IO supports the following:
0
: Error1
: Warn2
: Info3
: Debug and defaults to 3
Further information on configuring Socket.IO can be found at:https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO.
Let's now use our Socket.IO server and create a handler for Socket.IO ./lib/socket/handler.js
.
We start by importing the Socket
module, instantiating it, and passing it an Express-enabled httpServer
parameter. We create a Redis Subscriber
module and define a SocketHandler
function that accepts httpServer
as input. We set up a Socket.IO handler for the connection event. When ready, this will return the connected socket.
We then subscribe to two Redis channels—issues
and commits
—and define a Redis handler for the new message event. This handler broadcasts a channel and a message to clients listening on the channel defined by message.projectId
.
We define a Socket.IO subscribe
handler, which allows a client to join or subscribe to events on a given project. We also define a Socket.IO unsubscribe
handler that allows a client to leave or unsubscribe to events on a given project. We also define an error
handler on Socket.IO, which logs any errors to logger
:
var http = require('http') , logger = require("../logger") , Socket = require('../socket') , Subscriber = require('../cache/subscriber') , subscriber = new Subscriber(); function SocketHandler(httpServer) { var socketIo = new Socket(httpServer) socketIo.sockets.on('connection', function(socket) { subscriber.subscribe("issues"); subscriber.subscribe("commits"); subscriber.client.on("message", function (channel, message) { socket.broadcast.to(message.projectId).emit(channel, JSON.parse(message)); }); socket.on('subscribe', function (data) { socket.join(data.channel); }); socket.on('unsubscribe', function () { var rooms = socketIo.sockets.manager.roomClients[socket.id]; for (var room in rooms) { if (room.length > 0) { room = room.substr(1); socket.leave(room); } } }); }); socketIo.sockets.on('error', function() { logger.error(arguments); }); }; module.exports = SocketHandler;
Now we can wire up Socket.IO to our ./lib/express/index.js
Express server. Let's import the SocketHandler
module, passing to it an Express server called httpServer
:
, SocketHandler = require('../socket/handler') .. var httpServer = http.createServer(app).listen(app.get('port')) socketHandler = new SocketHandler(httpServer);
18.227.111.33