server send push notification to client browser without using polling - browser

I am developing a web app in which I want that when some changes in database occurs, server sends response to particular client(like push notification). And I want this notification to be sent client's browser. I don't want to use polling.
What can I do?
I think it's possible using SSE, but I am not clear.
I want to know
Is it possible to send response to particular client without client's request(without polling).
How server will detect that particular client?
please help me.

There is Web Notification. And there you can see the browsers that support it.

Related

How could i send notfication app to web in node js i am new to nodejs please if you can

i have to send a notification on web when a event occurs on app
Assuming you are using ReactJS, you need to use WebSockets. Check this tutorial on how to achieve that.
If the event dispatches from server, you can use Socket.IO, but if there is no hurry in getting notifications you can request in AJAX form every (for example) 5 minutes from client side code, to check if there are any notifs or not.

Sending a notification to user using Node.js

I am looking for a solution to my problem. I have Node.js server serving my web application where user can log in. I want to handle a situation where one user A performs specific action and user B associated with this action gets real life notification. Is there a module that would help me or there is some other solution?
What you are describing is "server push" where the server proactively notifies a user on their site of some activity or event. In the web browser world these days, there are basically two underlying technology options:
webSocket (or some use socket.io, a more feature rich library built on top of webSocket)
server sent events (SSE).
For webSocket or socket.io, the basic idea is that the web page connects back to the server with a webSocket or socket.io connection. That connection stays live (unlike a typical http connection that would connect, send a request, receive a response, then close the connection). So, with that live connection, the server is free to send the client (which is the web page in a user's browser), notifications at any time. The Javascript in the web page then listens for incoming data on the connection and, based on what data it receives, then uses Javascript to update the currently displayed web page to show something to the user.
For server sent events, you open an event source on the client-side and that also creates a lasting connection to the server, but this connection is one-way only (the server can send events to the client) and it's completely built on HTTP. This is a newer technology than webSocket, but is more limited in purpose.
In both of these cases, the server has to keep track of which connection belongs to which user so when something interesting happens on the server, it can know which connection to notify of the event.
Another solution occasionally used is client-side polling. In this case, the web page just regularly sends an ajax call to the server asking if there are any new events. Anything new yet? Anything new yet? Anything new yet? While this is conceptually a bit simpler, it's typically far less efficient unless the polling intervals are spaced far apart, say 10 or 15 minutes which limits the timeliness of any notifications. This is because most polling requests (particularly when done rapidly) return no data and are just wasted cycles on your server.
If you want to notify userB, when both of you are simultaneously online during the action, then use websockets to pass message to a two-way channel to notify userB.
If you want to notify them whenever, regardless of online status, use a message queue.

nodejs get response from api without refreshing page

EDIT:
I started nodejs few days ago and i want to understand one thing.
Imagine that i have a nodejs web communicate with API, when i send request (offer for a specific user) i would like to know if user has accepted or declined the offer without refreshing the site. I know there is a way with AJAX, but is there any better solution how can i get state of the offer if it is accepted/declined (if something change)
Every advice is appreciated!
If you want to know in the client when the state of something on the server changes (at some indefinite future time), then here are three options:
You can regularly "poll" the server every so often with an ajax call asking for any updates on the offer. The server can then return the current state of the offer and the client can update the status in the current web page.
You can create a webSocket or socket.io (socket.io is an API on top of a webSocket) connection from the client to the server. This is a long lasting connection which allows the server to send data to the client at any time. So, anytime the server sees a change in the state of that offer, it can send an update to the client and the client can then modify the current page to show that change.
You can use the newer server-sent events which is an extension to http which allows a server to send data to a client to accomplish something similar to the previous option.

Implementing push notification using Node.js

I was curious to know that can we implement push notification to our client via our Node.js server just like we send emails using nodemailler in Node.js? If yes! Then how? Can anyone please briefly elaborate. Also a point to note that the push notification should be user specific just like our mails are which are distinguished by our mail id.
Thanks in advance.
You can achieve this using Firebase Cloud Messaging. Here's a high level overview:
Each client registers with FCM and sends it's unique token to your node.js server.
The server saves this token and associates it with the user.
When it comes time to send the push notification, you use the FCM node.js library to send a push notification to the token associated with the user you want.
Not sure if it is the best way.
You might open a port on server side and listen to that port from client side. I am talking about socket programming. New notifications will be sent through that open port.

Is it possible to receive webhook events in web extensions?

We need to get webhook events from a domain in the web extensions itself. That domain is not under our control.
We get the web extension's URL using browser.identity.getRedirectURL(). We have registered this as the webhook POST callback URL in other domain.
Is it possible to receive the webhook events whenever the other domain POSTs the data in callback URL? Would it be sufficient to intercept HTTP headers in order to get the data or would we need to have Node modules/servers inside the web extension?
No, not like you described. This is a pretty deep misunderstanding how webhooks and/or browser.identity work.
Your webextension is running on a client machine; it's not a webserver listening for connections (an extension can't do that at all).
So whenever some other machine that emits a webhook event tries to connect to the endpoint provided, whatever it connects to is not your extension.
You make an allusion to browser.identity.getRedirectURL() and seem to think that this is a real address that is assigned to your webextension and others can POST to it (and your extension be somehow informed about it).
This is not the case: instead, it's a "virtual" URL that the browser will treat specially if you (the browser) navigates to it. That request never actually leaves your machine to some server. No other client can connect to it (except for other browsers with the same extension - but again it will only ever reach them).
A solution for receiving webhooks would be to have an actual webserver somewhere that can receive them, plus some sort of push mechanism to inform your extension of the event:
A persistent WebSocket connection to your "receiver" server.
GCM push messaging initiated by your "receiver" server. Not for Firefox

Resources