Migrating socket.io from 0.9.x to 1.x, Problems wi

2019-06-23 02:29发布

问题:

So I am migrating my node application from socket.io 0.9.x to 1.x, and I am having issues with configuring the RedisStore. I had this working when using 0.9.x, but I can't figure out how to get it working with 1.x. The documentation appears to only work for 0.9.x. Here's the relevant portion from their documentation, which I followed and had working with the old version:

var RedisStore = require('socket.io/lib/stores/redis')
  , redis  = require('socket.io/node_modules/redis')
  , pub    = redis.createClient()
  , sub    = redis.createClient()
  , client = redis.createClient();

io.set('store', new RedisStore({
  redisPub : pub
, redisSub : sub
, redisClient : client
}));

From what I can tell the problem appears to be this part:

var RedisStore = require('socket.io/lib/stores/redis')
  , redis  = require('socket.io/node_modules/redis')

Those files do not appear to exist anymore within the socket.io module.

Also, I've been using their migration guide as a reference, but it has no mention of specific changes to how to configure the RedisStore.

Any help or ideas would be greatly appreciated. Thanks!

回答1:

In case anyone else has the same troubles that I did, here's how I got it working again..

First you'll need to install the socket.io-redis module:

npm install socket.io-redis --save

Then, from within your node app, you'll need to replace your previous socket+redis-related configuration code with the following:

var redis = require('socket.io-redis')

io.adapter(redis({
    host: 'localhost',
    port: 6379
}))

That's it!

Reference links:

https://github.com/automattic/socket.io-redis



回答2:

For socket.io > 0.9 this is done through io.adapter using socket.io-redis Check the link https://github.com/Automattic/socket.io-redis

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


回答3:

Ok. As far as I understood, this new version of socket.io (1.0.6, to be specific) does not have redis under socket.io/node_modules anymore, right?

So, the solution would be install socket.io-redis and use it instead. But, how the sub = redis.createClient() can be re-used, if running node complains that socket.io-redis does not have any createClient() method.

In the other hand, if I choose keeping with socket.io 0.9.16 (that was what I was using till now), I'm getting other issue: the client is reconnecting every 20 seconds (looks like it is something related with xhr-polling). And I could not find anything concerning this as well, so, that was why I decided to updade socket.io and for some reason, they decided to change things overhere...

Could anyone give some help making socket.io-redis and nodejs work again (using redis.createClient() or something similar)?