How to keep Pusher Client objects persistent across pages? - javascript

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?

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:
How to maintain a WebSockets connection between pages?
Do Shared Web Workers persist across a single page reload, link navigation

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.

Related

Websocket with AngularJS/Asp.net

I had a specific questin about angularjs with websocket. I currently have an application that utilizes a websocket to communicate with a server, this is all nice and dandy - and when I move around pages in angular the websocket persists throughout all of the routes which is neat. Unfortunately the problem is that if the user refreshes the page (for some dumb reason), the websocket disconnects. I was wondering what the best method of handling this is. Should I just have an alert when the user tries to refresh, can I somehow detect that the websocket is closed when the page is refreshed and start a new one? I'm just wondering what the best practice for something like this is.
Thanks
There is nothing you can do, if the user refreshes, it is like restarting an application, all the bootstrapping happens again and connections are created again.
You can use javascript:onbeforeunload to warn the user that if refreshes or leaves he will lose the connection. But your users will hate your for that, it is very annoying.
Consider as well, that the user may open several tabs.
Starting a new connection is the best way. Just make sure that the user can somehow recover his context. If there is a different context per tab, then you will have to put a connectionID parameter in the URL to persist it through refreshes, and if the context is per user session, then a cookie with the session ID will do.

How to avoid websocket reconnection

I am currently building a WebSocket-based instant chat web application, it has more than one page. Every time users refresh or click a link to another page, WebSocket has to be reconnected. Is there a solution to avoid this?
If you refresh the page it will need to reconnect, i dont think there is a way around it.
I would recommend not using page-load to navigate between pages, but to build a single page application.
If you cannot do that in any way, maybe you can have your application in a frame, and the outer html will have the socket. that could work.
You might be interested in this blog post: WebSocket - persistent across page loads?
This discusses the issue and options in depth.
Disclosure: I work for Tavendo.

Socket.IO with multipul tabs of browser

I am using socket.io in my node.js application to give real time experience to my users. But due to leak of my experience with socket.io, I have some doubt with browser tab management. Let me explain first.
My website does not allowed login to user from multipul browser at a time. means If someone login from one browser, and then try to login from another browser, I have to kill previous login session. Means my socket.io emit messages to previous browser's all tab for logout, but my second browser's all tab should not get message for logout. How Do I do this?
Another question is I want to count distinct logged in users for my deshboard. But with multipul tabs, count is showing wrong figger. (eg. Single user accessing website from single browser but from 2 or more tabs, on server, socket client is showing each connection for tab, Here I need just one count. How Do I get it?
If some one has example/sample regarding above user case, please share it, so new coumer will gets help from it.
thanks

Persist Sockets over page refresh and page changes

I have my NodeJS and Socket.IO server running perfectly, however I notice that a socket disconnect and reconnect occurs every time I refresh the browswer page or navigate to a different page.
How do I make it so sockets persist between pages?
Thanks, I thought of that but wanted to avoid it as it's quite a big change for the system I'm making and would be a fair bit of work.
The only thing the socket connects/disconnects will effect is a "users logged in" counter. But I think I can get around that with a setTimeout on the Node server, so if a socket disconnects and reconnects within 2 seconds, the counter won't change. Is that the best work around in terms of this counter?
You can't really do that: network connections do not survive page navigation or refreshes. If you really really care you'll basically need to build a single page application.
you could use AJAX to navigate between the pages, to avoid the page refresh/socket disconnect

Is there an xmpp client that can persist chats across page refreshes?

We have a book reading application and I have been tasked with implementing basic chat functionality. We have chosen openfire as the chat server. My question is, while reading a book, when the user turns the page, this does a complete page refresh. How can I keep the chat going across those refreshes? I don't want to bump a user and make them rejoin. Any ideas of the path I should take to implement this? We would in theory like to drop in a client into the page and it works with minimal effort. Anyways, are their clients that persist across refreshes?
Web pages are stateless. They cannot by themselves propagate data from one load to another. For that, you need to use cookies, and/or server-side sessions. Once you've got a user logged in and a login cookie/session token established, you can SIMULATE the chat being unbroken.
Basically, you keep the state of the chat in the user's session file, and update as necessary. That way, whenever the page is reloaded or they navigate to another page, the chat's state "just follows along", making it appear it was never gone.
Use AJAX + postate/onhashchange effect for all of the pages on the website. This way the page itself (the view) can change, but the content wrapping around it (header + footer) won't change.

Categories