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

Manual disconnect and reconnect to same URL #6510

Closed
moehlone opened this issue Feb 8, 2015 · 9 comments
Closed

Manual disconnect and reconnect to same URL #6510

moehlone opened this issue Feb 8, 2015 · 9 comments

Comments

@moehlone
Copy link

moehlone commented Feb 8, 2015

I want to disconnect manually from a socket and to a later time make a new connection to the same URL. At the moment it is not possible with sails.js - reconnection always fails.

Is there any possibility to solve this with sails.io.js?
See: socketio/socket.io-client#251

socket.io implemented in 1.0 a param called 'forceNew' for io.connect()

Best regards,
Phil

@moehlone moehlone changed the title Manual disconnect and reconnect Manual disconnect and reconnect to same URL Feb 8, 2015
@theblang
Copy link

theblang commented Feb 9, 2015

@moehlone I was just about to ask this same question. In my case I want to unsubscribe from all models on log out, so I was hoping to just disconnect then reconnect the sails.io client. I got it to disconnect using io.socket.disconnect, but I can't figure out how to reconnect.

@eulercodex
Copy link

I'm having the same issue as well.

@theblang
Copy link

@eulercodex I used location.reload to accomplish this. If you are doing a SPA and don't want the flicker of a refresh, pass false for forceReload. The reload will happen from cache, but Sails.io will still reinitialize. Personally, I was wanting to reset all of the subscriptions to the pubsub system on logout; this seemed to do it.

@eulercodex
Copy link

@mattblang I don't particularly use sails. The issue was with socket.io but after looking at the doc online 1.2 release note, I was able to solve my problem this way

// on initialization
socket = io.connect({forceNew: true});
// for disconnection
socket.io.disconnect();
// for reconnection
socket = io.connect({forceNew: true});

@ghost
Copy link

ghost commented Mar 11, 2015

io.connect({forceNew: true});

works just fine

@ghost
Copy link

ghost commented Mar 11, 2015

if you are using sails you need to edit sails.io.js SailsSocket function:

   function SailsSocket (opts){
      var self = this;
      opts = opts||{};

      // Absorb opts
      self.useCORSRouteToGetCookie = opts.useCORSRouteToGetCookie;
      self.url = opts.url;
      self.multiplex = opts.multiplex;
      self.transports = opts.transports;

      // Absorb forceNew opts to force a new websocket connection every time
      self.forceNew = opts.forceNew;

      .
      .
      .

@muhammadghazali
Copy link

if you are using sails you need to edit sails.io.js SailsSocket function:

function SailsSocket (opts){
var self = this;
opts = opts||{};

  // Absorb opts
  self.useCORSRouteToGetCookie = opts.useCORSRouteToGetCookie;
  self.url = opts.url;
  self.multiplex = opts.multiplex;
  self.transports = opts.transports;

  // Absorb forceNew opts to force a new websocket connection every time
  self.forceNew = opts.forceNew;

  .
  .
  .

@ghost @eulercodex I had the similar problem here, and seems this forceNew option does the job well...

@sailsbot
Copy link

Thanks for posting, @moehlone. I'm a repo bot-- nice to meet you!

It has been 30 days since there have been any updates or new comments on this page. If this issue has been resolved, feel free to disregard the rest of this message. On the other hand, if you are still waiting on a patch, please:

  • review our contribution guide to make sure this submission meets our criteria (only verified bugs with documented features, please; no questions, commentary, or bug reports about undocumented features or unofficial plugins)
  • create a new issue with the latest information, including updated version details with error messages, failing tests, and a link back to the original issue. This allows GitHub to automatically create a back-reference for future visitors arriving from search engines.

Thanks so much for your help!

@twhtanghk
Copy link

The following way for power saving or reducing network traffic works fine in my case for platform browser or android. Also, it is not necessary to redefine the event handler for 'connect' setup in app config.

# power saving or reduce network traffic
document.addEventListener 'pause', ->
    io.socket._raw.disconnect()

document.addEventListener 'resume', ->
    io.socket._raw.connect()

@raqem raqem transferred this issue from balderdashy/sails.io.js May 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

6 participants