Node.js - socket.io - refresh page and disconnect - node.js

I use node.js and socket.io. When i do refresh the page - the user connection is interrupted. How do I solve this problem?
I do not want, when the user to switch between pages disconnected.
Thanks for the advice!

Personally, I would go for the single page application approach myself. But just fancying an alternative solution..
You could try using socket.io with an essentially hidden frame. In one frame, you maintain your connection while the user is still able to navigate in the other.
You'll have to have some gluecode to pass messages between the frames, but that should be trivial I think.

Related

Update database before leaving a page in Node.js

I want to develop an attendance management page where I want to update my MongoDB database every time when the user leaves the page.
I found unload function in some answers, but cannot realize how to use it in Node.js.
How can I do this using Node.js framework in PhpStorm?
You would have to do it from server side, sending request from client before he leaves the page is a bad approach. There are many ways how client can disconnect without sending any request, like connection lost, killing process etc.
Maybe consider different approach, like sending an ajax request as an indication that user is still on page?
Or you could look around for existing solutions, maybe something like Using node.js to display number of current users
could help.

A way around a Chron script

Is there a way to do the following in a SPA:
Add User
Update list of users, for all other logged in users, with the added user if they are in the User List Module of the application. Do this without a chron job constantly checking if there is a new user.
Almost like forcing a response without a Request. Is this a Unicorn?
I am trying to see if there is an efficient way of doing this.
Technologies are NodeJS, Express, and React on the client.
You could try setting up a websocket connection between your logged in users and your server. That way, you would be able to push a change (like a new user being added) to all your clients. However, all the clients would still need to be constantly online. Try looking into socket.io as a gentle introduction to websockets.
You could also try looking into WebRTC and other P2P web technologies, though I'm not sure if that would help you in any way.

keep socket open in node + express

I am making a webapplication with multiple pages using nodejs, express & socket.io. One of the features is push notifications. This means socket.io should be running on every page. Connecting to socket.io on every page load takes a lot of time, which slows down the app.
Is there a way to keep the connection to the socket open?
One way would be to use ajax to render different pages inside my root page, but I think this will over complicate the app.
Is there a better way to implement this?
Are you sure you're handling the client side the right way ?
If you have a single page application, the browser should almost never reload completely the page. You should be using some client-side framework to handle SPA mechanisms (routing, templating, etc) like angularjs/backbone/ember/etc...
With a well formated SPA you load your app only once and it is kept alive without page reload as long as the browser tab is opened. So you weboscket would be created also only once. You shouldn't have any problem of this kind, your server side code is OK, it's just that you're doing it wrong client-side.
By the way, if you want to do handle only push data, you should take a look at the Server Sent Events, which is simplier/lighter that a full-duplex implementation like socket.io (which is a bit heavy)

NodeJS - Stream in what the server is doing

I'm creating a simple webpage with NodeJS that'll upload a picture, resize it, pull some information from the web about the picture, and then save to the database. Easy stuff, all of it done server side. Though I'm trying to write an new feature that I'm a bit lost on how to go about. What I'd like to do is 'print' to the client when it's started each step of the process I mentioned about.
Imagine it like a white box, and every time something happens on the server, a new line is written says what for the client to read. How would I go about this? Any help is appreciated!
Use socket.io or some other websocket library. When the page loads, open a connection (in the browser's javascript). On the server, as events happen, send them as socket.io messages. In the browser, as events arrive, set them into a "status" element or append them to a list or whatever. You should be able to find lots of examples of chat servers out there and just convert chat messages to progress updates and there you have your architecture.
http://howtonode.org/websockets-socketio
Try something, then post a code snippet.

Multiple scoreboard's with node/socket.io

I'm trying to build an app that uses node/socket.io for a scorekeeper to update a form on a webpage (home/away/time) and have those results stored on the server for all clients to receive.
I'm a relatively new to node and socket.io, so would this be the best way to solve this problem?
Thanks for any suggestions.
Not sure if I understood the question, however if you have an online form and you need to push notifications from the server to the clients (i.e. they see updates without having to click or reload the page), you can use socket.io, yes.

Resources