Skip to content Skip to sidebar Skip to footer

How To Create A Node Js Api For Which Users Can Subscribe To Listen To Events?

I am trying to create and node.js api to which users can subscribe to get event notifications? I created the below API and was able to call the API using python ,however its not cl

Solution 1:

You can't just postpone sending an http response for an arbitrary amount of time. Both client and server (and sometimes the hosting provider's infrastructure) will timeout the http request after some number of minutes. There are various tricks to try to keep the http connection alive, but all have limitations.

Using web technologies, the usual options for get clients getting updated server data:

  1. http polling (client regularly polls the server). There's also a long polling adaptation version of this that attempts to improve efficiency a bit.

  2. Websocket. Clients makes a websocket connection to the server which is a lasting, persistent connection. Then either client or server can send data/events of this connection at any time, allowing the server to efficiently send notifications to the client at any time.

  3. Server Sent Events (SSE). This is a newer http technology that allows one-way notification from server to client using some modified http technology.

Since a server cannot typically connect directly to a client due to firewall and public IP address issues, the usual mechanism for a server to notify a client is to use either a persistent webSocket connection from client to server over which either side can then send webSocket packets or use the newer SSE (server sent events) which allows some server events to be sent to a client over a long lasting connection.

The client can also "poll" the server repeatedly, but this is not really an event notification system (and not particularly efficient or timely) as much as it is some state that the client can check.

Post a Comment for "How To Create A Node Js Api For Which Users Can Subscribe To Listen To Events?"