Is there a way to make Using server-sent events persistent?

允我心安 提交于 2020-01-11 12:48:27

问题


I need to run a script that updates the user's browser once every second. My need is a one way communication from the server to the client.

To do that, I implemented server-sent events polling from my server to the user's client.

The problem is that the user is allow to open multiple connection to the server "by opening multiple browser tabs" witch is a problem as it adds an overhead on the server.

I would like to limit the connection that is coming from a single user to 1. Meaning a single connection to the server across all their tabs. One connection between a client and the server even if the user have 10 browser tabs open.

This is my server-sent event implementation

var evtSource = new EventSource('getMyMessages.php');
evtSource.addEventListener("getMessagingQueue", function(e) {
    console.log(e);
    var data = JSON.parse(e.data);
    processServerData(data);

}, false);

evtSource.onerror = function(e) {
    evtSource.close();
};

Is there a way to make this method of communication persistent?


回答1:


(This is an indirect answer: how to prevent the overhead of multiple tabs being open. You'll need some communication method between tabs if all of them getting fed the data is desirable, and the shared worker idea looks good for that.)

At the application level: Use a PHP session.

When you use a PHP session the PHP script locks it. This means no other scripts can access the session. In your case that is what you want. (In another recent question, Server-Sent Events Polling causing long delays, that was the cause of a problem.)

This is even better if you require they login to the session to be able to use it. (Otherwise someone who disables cookies can still access multiple tabs.)

The problem is that their other tabs just sit there, and (I believe) the PHP process is using up resources on the server, while it waits for the session to unlock.

You can get around that resource-usage problem by using a session variable to record that they have a current connection, and making sure you release the session lock (as described in the other question). You can then give the user a friendly error message when they try to connect more than once.

At the proxy/firewall level: Set a rule to not allow the same something to have more than one connection at a time to the same URL (your SSE script in this case). Redirect to them an error page explaining why they are not allowed to.

The obvious, but bad, choice for the something is their IP address. That sucks, because multiple users can be coming from the same IP address. You could use their user agent, but just as unreliable. Giving them a cookie, and looking for that is better. (You could even look for the PHP session cookie.)



来源:https://stackoverflow.com/questions/31143150/is-there-a-way-to-make-using-server-sent-events-persistent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!