Realtime data update to the client using Websocket in node js - node.js

I want to send ws message to the client when my sql sever database table values updated or new row inserted (real time data) for that client. how can I achieve this. I'm using Node js and Mssql for DB. what I tried before was checking every second to DB and sending the records to the clients. But the problem is when more users connected I'm getting database timeout error. Why because I'm checking db for every users every seconds. What can I do now how can I solve this.

Related

Getting realtime notification whenever a new friend request is received MERN Stack using Socket.io

I am trying to add a functionality in my web app where whenever a new friend request is received in the database (mongodb) then i get a notification through from backend (Node.js) to my frontend (React.js)
Now i researched about this functionality and get to know about socket.io but the problem is the solutions i found which were using socket.io were kind of a brute force according to me ,
In those solutions they were querying the database inside the socket.emit(),
Now according to me if I keep querying the database every 4-5 seconds is it a good approach to do that doesn't it put load on database?
What is the right way to do this?
What i have tried so far is finding a better solution than querying the database again and again till i get an update. But i had no luck ..
The best approach is to connect frontend with backend using websocket/socket.io and as soon as you add a new object the server should push the data to frontend. You don't have to run a database query every 4-5 second. Write a server push event in your data.save() function. So as soon as you create a new object, the backend sends data to frontend.

socketIO events with databases , Storing new message in database and emit socketIO event to client at the same time

I am building a chat application , I am using socket.io for real time chatting and mongodb for messages storing, but my question is how to store messages in database in sufficient way, there are two scenarios:
emit socketIO event to client then store message in database (but some error may occur while saving in database and message couldn't be saved to database and this is a bug in the application)
or store message firstly in database and after storing done successfully I emit socketIO event to client (but there is a performance issue with this approach because storing in database may take a while)
So is there a better approach? thank you all

Using Node.js and Mongoose: Is switching between multiple connections on a MongoDB server an inefficient alternative?

I have several clients. Each client has multiple data acquisition stations. Each station has multiple sensors. I need to store this data on a MongoDB server (using mongoose and Node.js). So, I thought about the organization that follows.
Each client has its own database inside the MongoDB server.
Within this database, each station has its own collection.
Data is sent to the MongoDB server via an MQTT broker (Node.js). So, depending on which client the broker receives the message from, I need to create a new connection to the MongoDB server (using mongoose.createConnection).
I'm not sure if this is the best alternative. I don't know if creating multiple different connections will slow down the system.
What do you think?

Real time MongoDB / Node.js

I have a MongoDB database, Node.js server and User Interface.
The connection between the three is working and data is being sent between them.
I am creating a real-time web application and I am using a setTimeout() feature to request data from database via the client's webpage every 4 seconds.
BUT the data that is coming back is the same everytime and isnt updating from the database.
It is acting as if there is only one MongoDB session and just sending the same set of data that is collected from when the server starts.
On the Node.js server code, I made sure that a connection is opened and close once the query has completed but it is collecting the same set of data every time and no new data that has come into the database. The only way I can get it to update the data from the database is to turn the server on and off to reconnect to the database.
Would using some thing like socket.io be better for the real time requests or is it possible to do real time data just with a setTimeout() feature on the client side and normal MongoDB collection queries?
Is there a way to constantly refresh the connection to the database?

Pull latest data from database using NodeJS

I want to use NodeJS for live graphs for that reason, I want to pull latest data from database using NodeJS and then push to UI. Is there any possible solution in NodeJS.
I am using Cassandra database.
This question is very broad. If you want to poll your database on a timer you could use
setInterval(() =>{
// Read data from database here
// Send data to client
});
To send data to clients you could use a tool like socket.io which is easy to implement and works great with real time data! There is a great example here how to us socket.io to send data between a server and a client.
As mentioned in other answers you could set a timer that polls the database every X milliseconds and send out a update if there is new information. This will not be "real-time" but could be enough for your need. This will also waste som performance because of polling.
What you could do to solve this is to send out updates to the client at the same time you add content to your database. You could use http://socket.io/ to push updates out to the client.
So when you do something like:db.insert(data) you also io.emit(data) and send it to your clients at "real-time"

Resources