Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to update redis URL on fly #348

Open
benoitDD opened this issue Jul 4, 2020 · 2 comments
Open

How to update redis URL on fly #348

benoitDD opened this issue Jul 4, 2020 · 2 comments
Labels
question Further information is requested

Comments

@benoitDD
Copy link

benoitDD commented Jul 4, 2020

Hello,

How can I update redis URL on fly ?

I use:
node 10.16.0
express 4.17.1
socket.io 2.3.0
socket.io-redis 5.2.0
socket.io-client 2.3.0

I try to replace with a new Adaptater and set it to socket server:
Server

var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
var redisAdapter = require('socket.io-redis');

//the old adaptater.
io.adapter(redisAdapter({ host: 'localhost', port: 6379 }));

http.listen(3000, () => {
   console.log('listening on *:3000');

   setInterval(() =>{
      io.emit('bar')
   }, 1000)

   setTimeout(() => {
      //the REDIS URL is updated, so I must update the Adaptater with the new URL of REDIS.
      io.adapter(redisAdapter({ host: 'localhost', port: 6378 }));
   }, 10000)
})

The redis instances in port 6378 and 6379 work.

My issue: after updated the REDIS URL, the clients socket.io (socket.io-client) don't receive the event bar (and any other events !)

Before to update the REDIS URL the clients had received the event bar.

Client

var io = require('socket.io-client')
var socket = io.connect('http://localhost:3000')
socket.on('bar', () => console.log('receive'))

After the update of REDIS URL, to receive this event, the clients socket.io must to disconnect and reconnect. (it's bad!)

@benoitDD benoitDD changed the title How update redis URL on fly How to update redis URL on fly Jul 4, 2020
@darrachequesne
Copy link
Member

Hi! You're right, hot-reloading an adapter is not currently supported. The rooms and sids objects, which store the relationships between sockets and rooms, are lost when using the new adapter. A quick workaround:

setTimeout(() => {
  const rooms = io.of('/').adapter.rooms;
  const sids = io.of('/').adapter.sids;

  //the REDIS URL is updated, so I must update the Adaptater with the new URL of REDIS.
  io.adapter(redisAdapter({ host: 'localhost', port: 6378 }));

  io.of('/').adapter.rooms = rooms;
  io.of('/').adapter.sids = sids;
}, 10000);

I'm not sure this is a common use case though.

@darrachequesne darrachequesne added the question Further information is requested label Jul 9, 2020
@JordanPawlett
Copy link

I'm currently investigating an issue where I have a Sentinel Redis setup that my socket-io server is connecting to using this adapter.

In the situation that the sentinel master is 'destroyed', a slave is promoted to be the new 'master'.
The adapter seems to never successfully connect to the new master, and starts throwing different timeout errors.
e.g. Error: timeout reached while waiting for clients response.

I'll get a dev environment setup and investigate if i can connect to the new master. This seems like a good solution if all else fails. Any other pointers in the right direction would be greatly appreciated, thank you @darrachequesne!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants