drogonframework / drogon

Drogon: A C++14/17/20 based HTTP web application framework running on Linux/macOS/Unix/Windows
MIT License
11.53k stars 1.11k forks source link

http server responding using server sent events to a browser's eventsource api #1295

Open eniv opened 2 years ago

eniv commented 2 years ago

Hi, I'm looking for advice on how I should have my http server respond to a browser eventsource() with keep alive where the server should send notifications on occasion to the client browser. I have tried having the drogon server respond using an event/text-stream header with a data field. The browser will then receive and recognize the response, but it seems that then the connection disconnects (the eventsource error handler triggers and the browser retries again after 5s). I'm aware of the new stream response, but I'm looking for something slightly different. The stream response sets up a callback which gets called without an option to throttle and is therefore not suitable for sending a notification only once in a while. Thanks in advance. -Eyal

an-tao commented 2 years ago

I think you should use websocket as a long-live link for sending notifications.

eniv commented 2 years ago

Thanks for the suggestion. I was considering that but it is less elegant in my application where some logic inside an http controller class is related to the notification that should be sent out.

With an eventsource and sse (server sent events) I only need to add a route and I have access to that logic, whereas if I bring in an additional controller (websocket) I need to pass this information to it which couples them together.

an-tao commented 2 years ago

Thanks for the suggestion. I was considering that but it is less elegant in my application where some logic inside an http controller class is related to the notification that should be sent out.

With an eventsource and sse (server sent events) I only need to add a route and I have access to that logic, whereas if I bring in an additional controller (websocket) I need to pass this information to it which couples them together.

You could make a pub-sub service to decouple them. There is a pub-sub abstract class in drogon.

eniv commented 2 years ago

No easy way to do this with the current framework then? I've tried extending the lifetime of the callback and the http response beyond the scope of the http async method callback but the connection still seem to drop.

an-tao commented 2 years ago

No easy way to do this with the current framework then? I've tried extending the lifetime of the callback and the http response beyond the scope of the http async method callback but the connection still seem to drop.

No, you can't do that. currently the SSE is not supported by drogon, requests and responses must be in one-to-one correspondence.

eniv commented 2 years ago

I understand. Thank you very much for your comments.

Iamnotagenius commented 5 months ago

I think this issue should still be opened until SSEs are implemented in the framework.