-
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
Server Sent Events example - issue #7008 #7012
Conversation
Illustrates the use of SSE using ESP8266WebServer
CI has this error (on
Style check can be solved by the script in |
You also need, in this PR, on your local repository, to remove the submodule update you added by error: Probably with
and |
Done, apologies for my mistake and for wasting developer cycles, there is so much I have to learn about GitHub... |
Did you push ? |
Yes, https://github.com/ewaldc/Arduino is fully up to date and equal to my local repository. |
@ewaldc |
@ewaldc |
@ewaldc
|
Fix missing variables in printf statments Fix subscriptioncount not decreasing Fix SSEBroadcastState (argument sequence wrong)
@Martin-Laclaustra,
Originally I added the webclient, but I felt that I should then also post a Javascript example to make it all work, which in turn would not fit into one single .ino file... Hence I settled for just using curl and adding the HOW TO's just in comments. Maybe I took the lazy way out here :-) Hope it helps. |
Thank you. I will have a look.
It is true that you can not unregister in the ESP server, but that is not necessary. My solution pre-registers the channels (I am assuming they are a reasonable number). "Hundreds of active callbacks" Do you expect hundreds of concurrent channels handled by the ESP? My current example works with single figure channels but it can be easily extended to handle two figure channels.
Why do you not consider the one that I uploaded in my previous post? It is kind of a minimal-size example in a single file. You could paste it instead of the include (although I think it is clearer as a separate header file). It is only 58 lines. It can even be further shortened if some unnecessary html or javascript code is removed from it. Thank you for your attention. |
Subscription channels are rarely implemented with just a simple digit as in the example. For example in Eclipse Smarthome, OpenHab and several other IOT frameworks, a (unique) UUID is being used. Security is an important factor in their decision to do so (e.g. avoid unwanted listeners). This invalides pre-registration based solutions. In addition, some clients register an event listener for each page of their GUI (to listen for object updates) and unregister after the user leaves that page (e.g. OpenHab Android/IOS client). So yes, in a typical day my application handles literally hundreds of subscriptions, some days closer to a thousand. Besides not being able to unregister (causing the ESP to run out of memory), the ESP web server will execute a string compare for every single registered subscription. When dealing with e.g. long paths (not almost in my control) ("http://myiotserver.myverylongdomainname.net:48888/rest/events/listener/") we found out (the hard way) that using server.on() for each subscription consumed not only a lot of RAM (e.g. due the use of the String class) but can slow down the web server to unuseable levels, including hitting soft WDT resets. Hence, why I reverted back to doing one single string comparison to find out whether we are dealing with an event callback or not. In fact, I ended almost not using server.on() at all, saving lots of memory. If you have hundreds of IOT objects, each of which can be queried by "/rest/item/" , this issue of excessive server.on() calls/string comparisons/memory use only gets worse. If the ESP Arduino library would handle URL patterns, or match the beginning of the string e.g. server.onStartsWith() that would be a whole different case of course. I actually made a PR to implement such a function, plus the possibility to unregister, made modifications to the String class to avoid excessive copying etc but before I knew, it had become a massive PR which was difficult to explain, unless I reverted back to posting 20 or so individual issues, each with their own PR and code change. Rightfully so, the maintainers keep a close eye on compatibility and memory footprint, so it's not obvious to defend the addition of new functions. Hopefully, this gives some insights in why things were done differently, even though you have a very valid point that there are simpler and "less-lines-of-code" ways to make the example work. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR also pulled in a bunch of old submodules (i.e. a git commit -a
without a prior git submodule update
and so it's busted.
Can you please either remove the erroneous submodules included in the PR, or close this one and make a new one with only your single example file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Comments are good and the code style looks fine. Seems like something useful in the examples.
libraries/ESP8266WebServer/examples/ServerSentEvents/ServerSentEvents.ino
Outdated
Show resolved
Hide resolved
libraries/ESP8266WebServer/examples/ServerSentEvents/ServerSentEvents.ino
Outdated
Show resolved
Hide resolved
libraries/ESP8266WebServer/examples/ServerSentEvents/ServerSentEvents.ino
Outdated
Show resolved
Hide resolved
libraries/ESP8266WebServer/examples/ServerSentEvents/ServerSentEvents.ino
Show resolved
Hide resolved
libraries/ESP8266WebServer/examples/ServerSentEvents/ServerSentEvents.ino
Outdated
Show resolved
Hide resolved
libraries/ESP8266WebServer/examples/ServerSentEvents/ServerSentEvents.ino
Outdated
Show resolved
Hide resolved
libraries/ESP8266WebServer/examples/ServerSentEvents/ServerSentEvents.ino
Outdated
Show resolved
Hide resolved
libraries/ESP8266WebServer/examples/ServerSentEvents/ServerSentEvents.ino
Outdated
Show resolved
Hide resolved
libraries/ESP8266WebServer/examples/ServerSentEvents/ServerSentEvents.ino
Outdated
Show resolved
Hide resolved
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
Avoid duplicating WiFiClient by using the WiFiClient object embedded in the subscriber[] array instead.
Illustrates the use of SSE using ESP8266WebServer
edit from maintainer: closes #7008