Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to have one server-sent-event running per user not per tab?

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?

like image 266
Junior Avatar asked Aug 28 '15 18:08

Junior


People also ask

How are server-sent events implemented?

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.

Are Webhooks server-sent events?

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.

Is server-sent events a protocol?

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 ...

Is SSE long polling?

SSE is similar to the long-polling mechanism, except it does not send only one message per connection.


1 Answers

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.

like image 172
Ghedipunk Avatar answered Oct 22 '22 22:10

Ghedipunk