I am running into many issues because my users use multiple browser's tabs to use the same application.
The problem is that each tab will make a separate connection to the server to start server-sent events and the server will run a loop to fulfill the request. (if there are 5 tabs open per user then the server will have to start 5 different server-sent-event to respond!)
Is there a way to add some sort of logic to check if there is a connection established between a client and the server-sent script use the same connection instead of creating a new one?
I think this would be the same idea of using a WebSocket. However, the problem with me using WebSockets that each user must be authenticated using the server-sent event and not sure if this is possible with WebSockets. When a user logs in to the app, I generate a sessionID and the session checks their IPs/agent data to match before they are allowed to use the site.
How can I minimize the connection to the Server-Sent Event to 1 per user?
A connection over SSE typically begins with client-initiated communication between client and server. The client creates a new JavaScript EventSource object, passing the URL of an endpoint to the server over a regular HTTP request. The client expects a response with a stream of event messages over time.
When should I use Server Sent Events as opposed to Webhooks? Webhooks requires a server side subscription, meaning that you need an HTTPS endpoint to create the subscription. On the other hand, Server Sent Events subscriptions can be created on the client side.
SSE is a high-performance transport for server-to-client streaming of text-based real-time data: messages can be pushed the moment they become available on the server (low latency), there is minimum message overhead (long-lived connection, event-stream protocol, and gzip compression), the browser handles all the ...
SSE is similar to the long-polling mechanism, except it does not send only one message per connection.
The HTTP headers (and thus, cookies) are available to the WebSocket server while setting up each client's connection.
However, since you're not using WebSockets (yet?), the next best place to go is your client Javascript.
Cookies are available there, unless the cookie has the HttpOnly flag set. This will be your most reliable way to match a user across multiple browser tabs.
The browser user agent string plus their IP address is tempting, but keep in mind that most household routers use NAT, and many people who are living together will tend to use the same browser on their different computers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With