Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Mobile Service and socket.io

I am attempting to create a HTML5 / JS app and use an Azure Mobile Service for my backend.

Based on ScottGu's post where he demonstrates source control and npm module support, I am wondering if it possible to use socket.io to enable realtime notifications and the use of WebSockets.

I can see how one may be able to bring in the module and make use of it for each specific request (e.g. when a client POSTS to a resource, add a hook to broadcast the resource's creation to all clients) but I am unsure how to set up and share the socket.io object.

NB. I am aware of the built-in support for push based notifications for iOS, Windows, and Google but it doesn't (yet) provide an out of the box solution for web based projects - hence wanting to use socket.io (or any SignalR-esque equivalent).

like image 969
mattgi Avatar asked Oct 13 '13 01:10

mattgi


2 Answers

WebSocket should be fine on Azure Mobile Services, because it is just "downgrading" a HTTP connection back to a Socket with framing (more like reliable UDP). But there are few caveats:

  • If you are using Socket.IO, browsers/proxies doesn't support WebSocket will default fallback to XHR. But Socket.IO over XHR requires lots of URL endpoints and will fail on Azure Mobile Services. Details of Socket.IO protocol spec at https://github.com/learnboost/socket.io-spec
  • You need to use Redis (redis.io) to support server farm over Socket.IO, which require a Linux box. They only have experimental build on Windows platform for now

I would rather setup a different set of machines dedicated for Socket.IO server so it don't fail on browsers/proxies that doesn't support WebSocket. Then when someone POST to API on Azure Mobile Services, the API will queue a message to a message queue and signal all your Socket.IO servers to broadcast the message.

like image 60
Compulim Avatar answered Nov 18 '22 04:11

Compulim


Support for Socket.IO has been added using startup script extension

var path = require('path');

exports.startup = function (context, done) {
    var io = require('socket.io')(context.app.server);
    io.on('connection', function(socket){
      socket.on('chat message', function(msg){
        io.emit('chat message', msg);
      });
    }); 

       context.app.get('/public/chat.html', function(req, res) {
        res.sendfile(path.resolve(__dirname, '../public/chat.html'));
    }); 
    done();
}

For details see: http://azure.microsoft.com/blog/2014/08/26/how-to-use-socket-io-with-azure-mobile-service-node-backend/

like image 1
Muhammad Hasan Khan Avatar answered Nov 18 '22 03:11

Muhammad Hasan Khan