0

In Socket.io, I know how to connect to a specific URL with io.connect("http://example.com/"). However, it doesn't seem to work if I've already connected to a server.

Basically, I would like to change the server that I'm connected to (say, if the current one goes offline). So how would I do this? Would I need to disconnect first. If so, how?

Thanks!

Luke
  • 2,038
  • 3
  • 22
  • 46
  • 1
    You should put your servers on the same hostname so that the normal reconnection logic will work, should your server go offline. – Brad Nov 04 '16 at 21:30
  • @Brad Sometimes a server may crash or restart, meaning that it will reconnect to that server automatically. However, I will have multiple servers (and server IPs), so if a server is goes offline and doesn't get restarted, then it should connect to one of the other servers. – Luke Nov 04 '16 at 22:16
  • 1
    There's absolutely no reason they all can't share a hostname though. You could have thousands of servers all behind the same hostname, so that when one is unavailable you go to the next. You can do this with simple DNS records to a point. Beyond that, you can use a better DNS provider to factor in availability. – Brad Nov 04 '16 at 22:23
  • @Brad, +1, good point. – Luke Nov 04 '16 at 22:52
  • Just do `socket2 = io.connect(someOtherURL2)`. You will have to enable CORS in order to be able to connect to a cross origin host. – jfriend00 Nov 05 '16 at 04:34

0 Answers0