Multiple scoreboard's with node/socket.io - node.js

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.

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.

Update a web page using socket.io when database is updated by another page

I'm starting playing with NodeJS and Socket.IO and I've got a question regarding communication between pages of my application.
I've got a Node JS script (get_new_data) which is periodicaly executed using a cron task. This script will get information through Internet, parse them and write them in a PG Database. This is ok.
I've got a secondary script (show_last_data) using Node/Express/Socket.io that displays the last information of the DB when it is executed. This is also ok, right now.
What I would like now, is that the script show_last_data would be notified when new data are inserted in the database and the corresponding display divs to be updated.
Can get_new_data connect to show_last_data even if it is not the server that produces the page ? Or should I manage this in another way ?
Thanks for help
I've finally found a way to manage this using socket.io-client.
This page was particulary helpful to solve my question : socket.io as a client

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.

Node.js send variable to client

I have a small Node.js HTTP server that does requests to a mongo database (with the mongoose module).
What I want to do is query the database, store it in a variable (array) and send it to the client.
Because ideally, when the user clicks on one of the buttons on the html page, the JavaScript will pick-up the event and change the appearance of the website by showing calculations based on data that is stored in the database.
The only way I could come with was just "transferring" the database content to the client browser but if anyone can come with another solution that would be fine too !
So basically my question is :
How can I pass a variable from the Node.js server to the client browser when serving a page ?
Thank you in advance !
If you will be doing more than a couple of these types of transfers, I recommend looking into Socket.IO.
It's a layer that provides quick and easy communication between Node.js servers and web front-ends, by abstracting web sockets when available, and falling back to other transports (such as JSON-P or Flash) when it's not available. Basically, you would call io.emit('something', {yourdata: here}), and it is easily received on the other end. All of the serialization is done for you.
http://socket.io/
Give their demo a shot to see how it works.

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

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.

Resources