Search code examples
node.jssocketsazuresocket.io-redis

Using socket.io-redis on azure web service


Currently I have deployed a node application to azure app service. My current app service plan has two core processor. And I have set nodeProcessCountPerApplication: 2 in iisnode.yml file. Now I have implemented socket.io functionality for real time update to the UI. With single process it works fine. When I use nodeProcessCountPerApplication: 2 problem starts. I am getting the following error-

{"code":1,"message":"Session ID unknown"}

I tried to solve this using socket.io-redis. Here is the code for using redis-

var io = require('socket.io')(server);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));

But getting the following error after the changes-

Error: Redis connection to localhost:6379 failed - connect EACCES 127.0.0.1:6379
    at Object.exports._errnoException (util.js:1008:11)
    at exports._exceptionWithHostPort (util.js:1031:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)

What am I missing. Appreciate your time.


Solution

  • Per my experience, this problem happens when the client is using XHR-polling transport, and the server is in the cluster mode. So I think the best solution is use WebSocket only. To achieve that, you could follow the steps below:

    1. Enable WebSockets using the Azure Portal
      Click the web app from the Web Apps blade, click All settings > Application settings. Under Web Sockets, click On. Then click Save.
      enter image description here
    2. Tell Socket.IO to use WebSocket only
      Replace the following code in client-end
      var socket = io();

      to this code:
      var socket = io({transports: ['websocket']});

    Hope it helps. Any further concern, please feel free to let me know.