Websockets in Node.js - Making them reliable and scalable

5,419 views

Published on

Published in: Technology, Education
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,419
On SlideShare
0
From Embeds
0
Number of Embeds
60
Actions
Shares
0
Downloads
41
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Websockets in Node.js - Making them reliable and scalable

  1. 1. WEBSOCKETS IN NODE.JS
  2. 2. ABOUT ME • • • • GARETH MARLAND @GARETHMARLAND HTTPS://GITHUB.COM/GMARLAND HTTP://WWW.DOTSTORMING.COM (EXAMPLE CODE)
  3. 3. WHAT SHOULD WE CONSIDER? • • • RELIABILITY SCALABILITY HOSTING
  4. 4. HOW CAN WE IMPLEMENT THEM? • • • SOCKET.IO – MOST POPULAR BUT LEAST RELIABLE SOCKJS – A REASONABLE ALTERNATIVE ENGINE.IO – RELIABLE BUT LOW LEVEL
  5. 5. EXAMPLE – IMPLEMENTING IN ENGINE.IO
  6. 6. 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(); } } }
  7. 7. 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)); }
  8. 8. 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); } }); });
  9. 9. 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(); }); });
  10. 10. 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; } } }; }; }
  11. 11. RELIABLE WEBSOCKETS!
  12. 12. PROBLEMS WITH SCALING • • WEBSOCKETS REQUIRE “STICKY SESSIONS” WEBSOCKETS ONLY EXIST ON THE SERVER THE CLIENT CONNECTED TO
  13. 13. CLIENTS CAN’T PASS MESSAGES BETWEEN SERVERS!
  14. 14. 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
  15. 15. PASSING MESSAGES WITH REDIS
  16. 16. 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); } }); });
  17. 17. 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); } }); });
  18. 18. SCALABLE WEBSOCKETS!
  19. 19. THANK YOU!

×