websockets in node.js - making them reliable and scalable

19
WEBSOCKETS IN NODE.JS

Upload: gareth-marland

Post on 24-Jan-2015

2.514 views

Category:

Technology


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Websockets in Node.js - Making them reliable and scalable

WEBSOCKETS IN NODE.JS

Page 2: Websockets in Node.js - Making them reliable and scalable

ABOUT ME

• GARETH MARLAND

• @GARETHMARLAND

• HTTPS://GITHUB.COM/GMARLAND (EXAMPLE CODE)

• HTTP://WWW.DOTSTORMING.COM

Page 3: Websockets in Node.js - Making them reliable and scalable

WHAT SHOULD WE CONSIDER?

• RELIABILITY

• SCALABILITY

• HOSTING

Page 4: Websockets in Node.js - Making them reliable and scalable

HOW CAN WE IMPLEMENT THEM?

• SOCKET.IO – MOST POPULAR BUT LEAST RELIABLE

• SOCKJS – A REASONABLE ALTERNATIVE

• ENGINE.IO – RELIABLE BUT LOW LEVEL

Page 5: Websockets in Node.js - Making them reliable and scalable

EXAMPLE – IMPLEMENTING IN ENGINE.IO

Page 6: Websockets in Node.js - Making them reliable and scalable

function connectSocket() { socket = eio("http://localhost:8080/");

socket.onopen = function() { socket.onmessage = function(package) { // Handling received messages }; };

socket.onclose = function() {     console.log("Disconnected from websocket");      connectionAttempts++;

if (connectionAttempts < 10) {     connectSockets();     } }}

SETTING THE CONNECTION ON THE CLIENT

Page 7: Websockets in Node.js - Making them reliable and scalable

function sendMessage(action, data) {var socketPackage = { action: action, data: data }this.socket.send(JSON.stringify(socketPackage));

}

var newMessage = {owner: “Gareth”,message: “Hello there”

}

var action = “greetings”;

sendMessage(action, data);

SENDING AN OBJECT FROM THE CLIENT

Page 8: Websockets in Node.js - Making them reliable and scalable

socket.on('connection', function (client) { client.send('123456');

client.on('message', function (data) { for (var key in socket.clients ) { if(typeof client.id !== 'undefined') { if(key == client.id) { continue; } } socket.clients[key].send(data); } });});

LISTENING ON THE SERVER

Page 9: Websockets in Node.js - Making them reliable and scalable

var app = express();

app.configure(function() {    app.use(function(req, res, next) {      res.header("Access-Control-Allow-Origin", "*");      res.header("Access-Control-Allow-Headers", "X-Requested-With");      next();    });});

REMEMBER TO ALLOW CROSS DOMAINS IN EXPRESS!

Page 10: Websockets in Node.js - Making them reliable and scalable

function connectSockets() { socket = eio(‘http://localhost:8080/’);

socket.onopen = function() { socket.onmessage = function(package) {    var socketPackage = JSON.parse(package.data);

      if (socketPackage.action != null) { switch(socketPackage.action) {           case ‘greetings’:

console.log(socketPackage.data.message);                 break;             }         } }; };}

RECEIVING AN OBJECT ON THE CLIENT

Page 11: Websockets in Node.js - Making them reliable and scalable

RELIABLE WEBSOCKETS!

Page 12: Websockets in Node.js - Making them reliable and scalable

PROBLEMS WITH SCALING

• WEBSOCKETS REQUIRE “STICKY SESSIONS”

• WEBSOCKETS ONLY EXIST ON THE SERVER THE CLIENT CONNECTED TO

Page 13: Websockets in Node.js - Making them reliable and scalable

CLIENTS CAN’T PASS MESSAGESBETWEEN SERVERS!

Page 14: Websockets in Node.js - Making them reliable and scalable

REDIS TO THE RESCUE!

• REDIS HAS A PUB/SUB QUEUE

• HAVE ALL OUR SERVER INSTANCES PUBLISHING AND SUBSCRIBING TO THE QUEUE

• PASS MESSAGES BETWEEN SERVERS USING THE QUEUE

Page 15: Websockets in Node.js - Making them reliable and scalable

PASSING MESSAGES WITH REDIS

Page 16: Websockets in Node.js - Making them reliable and scalable

var pub = redis.createClient();

socket.on('connection', function (client) { client.send('123456');         client.on('message', function (data) {

try {   pub.publish('subscription-channel', data);        }        catch (err) {            console.log("Error publishing: " + err);        }

});});

PUBLISH A RECEIVED MESSAGE THROUGH REDIS

Page 17: Websockets in Node.js - Making them reliable and scalable

var sub = redis.createClient();

sub.on('ready', function() {

sub.subscribe('subscription-channel');

  sub.on('message', function(channel, message) {

     for( var key in socket.clients ) {   socket.clients[key].send(message);     }

});

});

RECEIVING A MESSAGE FROM REDIS

Page 18: Websockets in Node.js - Making them reliable and scalable

SCALABLE WEBSOCKETS!

Page 19: Websockets in Node.js - Making them reliable and scalable

THANK YOU!