-
Notifications
You must be signed in to change notification settings - Fork 13.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhancement request: support for Server Sent Events aka EventSource #7008
Comments
Thanks ! |
Illustrates the use of SSE using ESP8266WebServer
@d-a-v,
Hence, no need for the enhancement request. Sorry for that, but I hope the example is still useful. I tried for days to make it work using WebSockets and AsyncWebServer but both solutions are much more complex, consume much more memory and could not be made stable. The one thing I have not figured out is why either of client.setNoDelay(true); or client.setSync(true); (lines 121/122 in the example) cause a WDT Soft Reset in the KeepAlive function when issuing client.println(), while the regular state event updates using a similar client.printf_P() continues to run without any problem! They both write to the same WiFiClient with IMHO very similar calls. Hope the example is still useful. |
I think you should try to use |
Thanks for the suggestion, this was already top of mind as it is not recommended to do networking stuff in Ticker.attach(), even though a "keepalive" packet is less than 100 bytes. Rather than posting a PR I will post the update code a link to e.g. pastebin. |
Just commit changes to your branch, it will update the PR |
Thanks for your flexibility, I have posted the updated example here
|
Example updated after feedback/review from users |
* Server Sent Events example - issue #7008 Illustrates the use of SSE using ESP8266WebServer * Update ServerSentEvents.ino * Create ServerSentEventsMultiClient.ino * sync * Update ServerSentEvents.ino * Update ServerSentEvents.ino Fix missing variables in printf statments Fix subscriptioncount not decreasing Fix SSEBroadcastState (argument sequence wrong) * Undo the library additions, move to current master * Fix compiler warning * Address review and fix multi-sensor updates Address points of @devyte's code review: * Use IPAddress vs. uint32_t * Refactor the URL parsing logic to use strlen vs. sizeof, since there was some confusion in the original (correct) version * Minimize copies of WiFiClients while in use * Use byref access for sensor updates Fix multi-sensor updates * Create an update Ticker for each sensor, because the original code only had one whose callback was overridden by sensorB, meaning sensorA never changed * Fix IPv6 build errors * Remove WiFiClient extraneous copy Avoid duplicating WiFiClient by using the WiFiClient object embedded in the subscriber[] array instead. Co-authored-by: Earle F. Philhower, III <earlephilhower@yahoo.com>
This is a request for support of HTML5 Server Sent Events aka Event Source.
It allows e.g. to implement an event bus over HTML.
The required changes to the ESP8266WebServer library are absolutely minimal: zero impact on existing code and increase in code size/memory footprint of just few bytes (basically sizeof bool).
Essentially, the variable bool keepCurrentClient which is currently hardcoded to false, becomes a protected class variable in the ESP8266WebServer class that can be modified via a new function called keepCurrentClient(bool).
To enable Server Sent Events/event subscription, it requires two web server instances, one being changed to keep the client socket/session (an event listener) open until disconnected.
Everything else can implemented in user code (event subscription, event bus keepalive messages, event payload etc.).
I have this currently working on a clean fork from master branch with just this one change.
Happy to post a PR (it's ready to post), if this is a reasonable request.
I can also contribute a working example, I suppose in the ESP8266WebServer examples section.
Thanks once again for this brilliant library and for taking the time to consider this request,
Ewald
The text was updated successfully, but these errors were encountered: