Websockets in Node.js - Making them reliable and scalable
Upcoming SlideShare
Loading in...5
×
 

Websockets in Node.js - Making them reliable and scalable

on

  • 2,191 views

 

Statistics

Views

Total Views
2,191
Slideshare-icon Views on SlideShare
2,139
Embed Views
52

Actions

Likes
4
Downloads
25
Comments
0

2 Embeds 52

http://www.scoop.it 48
https://twitter.com 4

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Websockets in Node.js - Making them reliable and scalable Websockets in Node.js - Making them reliable and scalable Presentation Transcript

    • WEBSOCKETS IN NODE.JS
    • ABOUT ME • • • • GARETH MARLAND @GARETHMARLAND HTTPS://GITHUB.COM/GMARLAND HTTP://WWW.DOTSTORMING.COM (EXAMPLE CODE)
    • WHAT SHOULD WE CONSIDER? • • • RELIABILITY SCALABILITY HOSTING
    • HOW CAN WE IMPLEMENT THEM? • • • SOCKET.IO – MOST POPULAR BUT LEAST RELIABLE SOCKJS – A REASONABLE ALTERNATIVE ENGINE.IO – RELIABLE BUT LOW LEVEL
    • EXAMPLE – IMPLEMENTING IN ENGINE.IO
    • SETTING THE CONNECTION ON THE CLIENT 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(); } } }
    • SENDING AN OBJECT FROM THE CLIENT var newMessage = { owner: ‚Gareth‛, message: ‚Hello there‛ } var action = ‚greetings‛; sendMessage(action, data); function sendMessage(action, data) { var socketPackage = { action: action, data: data } this.socket.send(JSON.stringify(socketPackage)); }
    • LISTENING ON THE SERVER 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); } }); });
    • REMEMBER TO ALLOW CROSS DOMAINS IN EXPRESS! 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(); }); });
    • RECEIVING AN OBJECT ON THE CLIENT 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; } } }; }; }
    • RELIABLE WEBSOCKETS!
    • PROBLEMS WITH SCALING • • WEBSOCKETS REQUIRE “STICKY SESSIONS” WEBSOCKETS ONLY EXIST ON THE SERVER THE CLIENT CONNECTED TO
    • CLIENTS CAN’T PASS MESSAGES BETWEEN SERVERS!
    • 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
    • PASSING MESSAGES WITH REDIS
    • PUBLISH A RECEIVED MESSAGE THROUGH REDIS 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); } }); });
    • RECEIVING A MESSAGE FROM REDIS 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); } }); });
    • SCALABLE WEBSOCKETS!
    • THANK YOU!