Long version: Iām trying to respond to item changes via a small local website and was glad openHAB has an excellent REST API. When I learned about the SSE I tried to get this to work, however the browser stubbornly refuses to output anything to me. The EventSource task has a pending in the network log, but no data is received.
The test via CURL delivers the desired result.
I just debugged a bit deeper and tried to rebuild the implementation from the BasicUI page.
As it turned out, a lib is used there, which provides the polyfill at the same time. Practical, but not what I need since I donāt use any unsupported browsers. (Link to the script: GitHub - Yaffle/EventSource: a polyfill for http://www.w3.org/TR/eventsource/)
Then I tried again to see how this looks like in API Explorer. But there I only get a perpetual loading spinner at the events.
Another try was testing different browsers (Google Chrome, Firefox): No luck
I also put the HTML page on the same host as the OpenHAB instance to avoid CORS errors, although I disabled CORS in runtime.cfg: No luck.
This call is used to set up a downstream SSE channel that uses another REST endpoint to request content. The data: 2176313b-40b1-4095-871e-27d73c748520 that it returns is then used by posting an array of item names, like ["Item1", "Item2"] to:
The SSE channel will then start receiving those events, you can continue to post new arrays of items using that id, and the persistant SSE connection will only stream those.
Thank you for the detailed explanation of the SSE downstream connection. Should consider including this in the documentation.
Unfortunately this knowledge does not help me. Because even if I set up a downstream connection I should at least get the āconnectionIdā, right?
Especially I find strange that in the API explorer at the normal āeventsā as well as at the āevents/statesā a permanent loading spinner appears.
Iāve tried a clean openhab installation on a bare linux PC with the same results. why does it work an the MainUI but not via API-Explorer or external request? The only difference i spottet is the used lib.
Do you mean in the browser? Loading a SSE endpoint directly from a browser is not how the SSE standard was designed to work, so the results will probably vary from browser to browser on how it displays a text stream of various formats and not html. Youāre better off loading the Main UI or the Classic UI and inspecting the event stream with the browser dev console. This specifically will recognize a SSE event stream and give a nice UI to view messages. Curl is also your friend here for quick prototyping.
Iām not sure what it was originally designed for, but according to the MDN documentation and my experience so far, itās a very convenient way to get updates from the server without having to constantly ask for them from the client side. And another advantage is that it is not bidirectional (ex: WebSockets), so no attack vector on the server.
But Iām not interested in fundamental discussions about which technology was originally developed for what and when
I donāt think that was made entirely clear in the opening post. I made a small web page as a dashboard because I wasnāt quite happy with the visualization options.
This can excellently control the items via the REST API, however for completion I need a way to keep the states of the items up to date without periodically checking on all of them.
In the API Explorer you can test the individual REST requests very well with the help of the āTry outā button, the following parameter settings and the final click on āExecuteā.
Only with the two āeventsā this does not work. There I get a loading spinner that never ends.
Similarly, when I make the call to the events from the external web server using JavaScriptā¦ I have an endless āpendingā and even if I restrict the topics to e.g. āopenhab/itemsā, I donāt get any messages.
Iām not arguing anything you are mentioning, i understand how sse works. It was not clear to me if you were just pointing a browser to the event URL, but it sounds like you are trying to use the API explorer for this.
I donāt think using the API explorer is going to work, the api explorer page is autogenerated from annotations in the core REST code, uses a off the shelf swagger UI, and probably does not have any concept of what an SSE stream is, and is not going to have javascript logic set up to use an EventSource and display the results (thereās no annotations i know of for SSE support).
My advice is to stick with using Javascript or curl to play with SSE.
When I request the /rest/events?topics=openhab/items in the browser the connection is set to pending, so I have an open stream.
However, no matter what action I perform in the openhab app, I do not receive any messages.
This is my main problem.