How to consume Server Sent Events(SSE) in NodeJS? - node.js

I'm getting Server Sent Events from a particular source but, I don't want to consume them directly in a browser (Client) instead, I'd like to consume those events in a server (NodeJS) and modify those event data and store the modified data in a database.
I have tried using eventsource node module but I couldn't achieve what I wanted...
Below depicted is the flow I want to achieve.
SSE events from third party ===> NodeJS server (consume them, modify them, store them into a DB) ====> (serve to the client) Client (any browser)
Any ideas!!!

Related

Nodejs - Transfer data between two server and client

I started to implement a HTTP ping health monitor as a private project with React and Node.js. I thought about making monitor with intervals that will send an axios request to server to receive all the urls and will return the results to server which will be shown later on in the client side.
I don't wanna use REST API to transfer data between the monitor and the server and to show it lively in the client side.
MONITOR <--> SERVER <--> CLIENT
What should I use instead of REST API in order to communicate between the monitor and the server? I know socket.io is fine to communicate between the client and the server but it is not so good for scaling.
What will be good and fast to transfer data for this specific project and not so hard to implement?
Thanks!
You can work with Server Sent Events in NodeJS, that is a way of receiving events from the server. Then you can use EventSource to open a connection to the server to begin receiving events from it.
https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
Take a look at this tutorial from DigitalOcean:
How To Use Server-Sent Events in Node.js to Build a Realtime App
Also take a look at Socket.io:
https://socket.io/

MongoDB change stream vs socket.io

I am working on a feature where the admin creates a course and the user gets notified. I am thinking of using MongoDB change stream but I need sockets to send the changed data to the frontend.
Now the scenario: Admin creates a course, change stream notifies and the socket.io emits that to the frontend.
My question is, if we are using socket.io to send the data to the frontend then why do we need MongoDB change stream? What we can do is simply emit the event when admin success in creating a course.
I have gone through multiple articles and they are doing the same- calling an API to create and update the document, a change stream to watch, and socket.io emit the event.
If we can send the data to the frontend once the course is created then why do we need MongoDB Change Stream?
You server side code may want to send this event to multiple destinations and/or trigger additional processes.For security reasons you need this event to be controlled by the server side. Of course you could always rely on the client to emit an event that the db update was successful, but I would still check that on the server side. Mongodb streams would allow you to listen on those events and take action without relying on the client.

How to send data to client with node express when client has not ask for?

My Question is above. I want that when an event triggered like a new Mail comes in or an other client has connected to the server, my application send to the existing connection a request(?) which say something like "Hey you not alone !". I know how to answer on request from client but how can i send the client information when he has not ask for it explicit.
i hav draw an simple image to visualize what i mean
You cannot directly do this. However, you can archive this by Server-sent Event which is making use of long-polling.
Reference:
https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events
As per my understanding you are looking for a way to send data / message from server to client side.
if so there are two ways to do it.
Create an API and pool is after certain interval of time from client side.
Create a socket server, connect your client with socket which will give you maintain the socketId on server

Streaming data from multiple api calls using nodejs and socket io

I am new to socket.io. I have a backend server implemented on nodejs which makes multiple async calls to rest api and send the response back to the client on angular 4.0. I follow the simple http request methodology. However, the issue is that it takes a while for the server to get collect and parse data from each multiple apis and send it back to the client. Therefore, I wanted to implement streaming which will return data to the client as soon as it gets from anywhere. I wanted to know if this can be done efficiently using web sockets and socket.io or is there another way around to do this.
I know, we can make async calls from the client but i want to implement the logic on the server rather than the client.
Note: I donot have realtime data

Retrieve Socket.io Client from Redis

I'm building a real time data system that allows an Apache/PHP server to send data to my Node.js server, which will then immediately send that data to the associated client via socket.io. So the Apache/PHP server makes a request that includes the data, as well as a user token that tells Node.js which user to send the data to.
Right now this is working fine - I've got an associative array that ties the user's socket.io connection to their user token. The problem is that I need to start scaling this to multiple servers. Naturally, with the default configs of socket.io I can't share connections between node workers.
The solution I had in mind was to use the RedisStore functionality, and just have each of my workers looking at the same Redis store. I've been doing research and there's a lot of documentation on how to use pub/sub functionality for broadcasting messages to large groups (rooms). That's fine, but I need to be able to send messages to a single client, so I need some way to retrieve a user's socket.io connection from the RedisStore.
The only way I can think to do this right now is to create a ton of 'rooms' named with the user's token, and only have one user in each room. Then I could just emit to that room. However, that seems very inefficient.
Is there a better way that I can retrieve user's unique socket.io connections from Redis?
Once a socket connection is made to a server running the node server, it is connected to that instance.
So it seems you need to make a way for your php server to know which node server a client is connected to.
In your redis store you could just store the id of the server as the value by the client id. Then php looks up which node server to use and makes the request.

Resources