Using socket.io-redis on azure web service

不羁的心 提交于 2019-12-25 04:27:28

问题


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.


回答1:


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.
  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.



来源:https://stackoverflow.com/questions/40112057/using-socket-io-redis-on-azure-web-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!