Limit parallel browser connections

Is it possible to limit the number of parallel connections from browser tabs opening the same workspace? (Maybe even with nice message in the browser)
It is required to reduce resources from users accidentally not closing old tabs.

[original thread by amiramw]

Hi! Each tab has only one web socket connection. Do you mean any other connections?

Although on the bad connection many connections can be opened one after another.

You can try to customize WebSocketConnectionProvider to avoid reconnections in the background or something like that.

[amiramw]

I mean I would like to avoid duplicate LSPs for example. As far as I can tell all of them are per connection.

Connections defines a user session right now, i.e. dirty documents and so on. Each window can have different set of opened documents and should have own LSPs.

In theory there is such thing like shared worker for the same origin which can reuse the same websocket connection.

But such approach will require decoupling a user session === a connection by multiplexing user connections over the same websocket for example.

Not sure how it will affect performance-wise, messages from one window could prevent processing of messages from another window because of sequential nature of websockets.

@amiramw I think you could add an express middleware injecting cookies into the requests, and work your way from here, identifying connections and responding with appropriate error codes? Not sure if you would have to also modify the websocket factories to prevent opening more past a certain point, or if the express middleware alone would be enough.

I’m not sure that limiting ws connections from the server is a good idea, especially if you can share your environment. One user reaches the limit, and another can not work anymore? I was suggesting to have custom WebSocketConnectionProvider which will close client ws connection for the background windows (maybe after some timeout) and reconnect it when a window gets visible again.

[amiramw]

How do you identify that a ws connection is from a background window?

I was suggesting to count based on tokens given to individuals via cookies, not counting each individual request independently.

This is basically how sessions usually work, to identify unique users. From there you can count the number of frontend served per user session and hopefully limit connections this way?

This is basically how sessions usually work, to identify unique users. From there you can count the number of frontend served per user session and hopefully limit connections this way?

How do we distinguish that a request is coming from the different browser or from a different window in the same browser? Another thing how we decide which window in the same browser should be limited on the backend?

How do you identify that a ws connection is from a background window?

There is browser API for this: https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API

btw a note from there:

The Page Visibility API is especially useful for saving resources and improving performance by letting a page avoid performing unnecessary tasks when the document isn’t visible.