'Is there a way of keeping a SSE connection but not run any code?
So basically I'm working with a server in PHP and client in JS and I need to implement a way of sending updates to clients.
There's a long polling that we want to replace for something more efficent so we were thinking in SSE but I want to send the data to a specific client after I do an INSERT into the database in a specific action.
So I have some questions about it
- Do I need to connect the EventSource from the client to the action that makes the INSERT to have access to it from there?
- It is possible to connect the EventSource to some action and not run the code in it?
Because I'll like to have the connection available to send data after the INSERT but I don't want to run the code every time the EventSource reconnect. Is any of this possible or should I use some other technology like WebSocket?
Thanks and sorry if my english sucks. Hope you understand my question.
Solution 1:[1]
Your SSE connection in PHP is a dedicated process (for each client). There has to be code there, but in your case it will be as simple as hanging around waiting for a signal that the INSERT has happened.
This is all likely to be identical to how your current long poll solution works?
There's a long polling that we want to replace for something more efficent ... should I use some other technology like WebSocket?
It depends on in what way long poll is inefficient, but I'd expect the bottlenecks to not change.
SSE is really just a HTML5 standard, wrapped around how long poll works. And WebSocket is the same, except it is two-way (and trading more flexiblity for more complexity). In all three cases you are keeping a dedicated socket open (*), and there has to be a process on the server handling that connection.
*: Less of an issue with http2 multiplexing.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Darren Cook |
