2

I'm just getting started with Pusher and so far everything is great.

But I realize that as my user opens and closes, or clicks on an internal link in my site, the connection automatically disconnects as the page unloads.

This would make the user connect and disconnect every time he navigates to a new page.

Is there a way to keep the user's subscriptions and connections persistent as he travels through my site?

Edit: Just to clarify, I understand the behavior that Pusher disconnects the moment the user closes his browser or page. I was just wondering how do we keep a connection alive if the person visits another internal link within the same site. Or is disconnection the best practice? If so, why?

leggetter
  • 15,248
  • 1
  • 55
  • 61
David C
  • 3,659
  • 5
  • 34
  • 46

2 Answers2

3

As @devnull69 says, for the moment you would need to create your application so that it didn't perform standard page navigations - you would build your app as a SPA (single-page-application). That way there are no page unloads and loads between page navigations.

It may be possible to persist a connection between page loads by using shared web workers. But as CanIUse demonstrates, browser support isn't perfect yet.

Pusher does have plans to offer message history which would mean that any messages missed during page loads can be fetched upon reconnection.

Also see:

Community
  • 1
  • 1
leggetter
  • 15,248
  • 1
  • 55
  • 61
  • Shared Web workers may get there, but with current Chrome (34.0.1843.0 canary), there is no persistence of a shared Web worker across page loads when there is only a single tab open to the domain. (You can get persistence when playing around with two tabs and then closing one, but that is probably an error rather than a feature.) – gzost Feb 16 '14 at 14:36
1

The only workaround would be to switch from regular page refresh to Ajax. This would enable you to keep up the websocket connection while loading only the part(s) of the page that change using Ajax requests.

devnull69
  • 16,402
  • 8
  • 50
  • 61