stepanic
stepanic

Reputation: 21

Azure Mobile Service - Socket.IO integration

I have problem with creating Azure Mobile Services Custom Script, I want to use Socket.IO Node.js module, but I don't know how to edit Azure Mobile Services route to be able to access /socket.io/1

After execution this code socket.io is started but client is not able to access URL endpoint from browser, please help me, thank you in advance, my email is: [email protected]

My code is:

in /api/notify

exports.register = function (api) {

    api.get('socket.io',getSocketIO);

};

function getSocketIO(req,res)
{
var app = require('express')()
  , server = require('http').createServer(app)
  , io = require('socket.io').listen(server);

server.listen(80);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

res.send(statusCodes.OK, { message : '23 Hello World!', bla: 'bla2' });
}

Upvotes: 2

Views: 455

Answers (3)

Muhammad Hasan Khan
Muhammad Hasan Khan

Reputation: 35146

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/

Upvotes: 1

mikermcneil
mikermcneil

Reputation: 11271

@stepanic, you might try bundling the Socket.io client as a static file. Here's how we do it in Sails for reference:

From the docs:

  <!-- .... -->
  </body>
  <script type="text/javascript" src="./path/to/bower_components/sails.io.js"></script>
  <script type="text/javascript">

    // `io` is available as a global.
    // `io.socket` will connect automatically, but it is not ready yet (think of $(document).ready() from jQuery).
    // Fortunately, this library provides an abstraction to avoid this issue.
    // Requests you make before `io` is ready will be queued and replayed automatically when the socket connects.
    // To disable this behavior or configure other things, you can set properties on `io`.
    // You have one cycle of the event loop to change `io` settings before the auto-connection behavior starts.

    io.socket.get('/hello', function serverResponded (body, sailsResponseObject) {

      // body === sailsResponseObject.body
      console.log('Sails responded with: ', body);
      console.log('with headers: ', sailsResponseObject.headers);
      console.log('and with status code: ', sailsResponseObject.statusCode);
    });
  </script>
</html>

Upvotes: 0

phillipv
phillipv

Reputation: 1717

Socket.io is not currently supported.

In may be possible to get it working, but you would need to do this code inside of your mobile services startup script, http://blogs.msdn.com/b/azuremobile/archive/2014/01/14/new-startup-scripts-preview-feature-in-azure-mobile-services.aspx, using the App object provided there.

You'd also need to update the routes on it so your routes are picked up before the mobile service ones.

Upvotes: 0

Related Questions