yetanotherhueapi icon indicating copy to clipboard operation
yetanotherhueapi copied to clipboard

Server-Sent Events

Open philipparndt opened this issue 4 years ago • 6 comments

The Hue API v2 (currently in beta) added support for push notifications using SSE.

This would be a great enhancement when working with buttons.

Do you have a plan to support this?

philipparndt avatar Jan 11 '22 20:01 philipparndt

Hey, thanks for submitting this as an issue! You are definitely right, the SSE would be an awesome feature to have and I'll definitely want the library to have it some day! 🤩 Unfortunately I haven't had much time to work on this library lately, but I'm hoping I'll get a bit more time starting next week. For now, what little time I've had, I've spent on #46 to add mDNS discovery support, as Philips is deprecating the UPNP discovery method.

While I'm hoping the SSE feature in itself might be a pretty straight forward one to implement, it would need more of the new v2 API to be implemented around it. For one, the SSE API identifies the devices with a UUID, which does not exist in the API v1. Also, when implementing the API v2, I would need to see if it can be compatible with the current interfaces that the library offers, or if I should just be bold and break all the backwards compatibility and release new major version (3.0.0) that would support the API v2 only.

For now I'm hoping I could add SSE into the current version without breaking backwards compatibility, but to fully support everything else that the new API v2 offers a new major version might be required. I would still need to explore and experiment with the new API to form a full picture of the situation.

Finally, I'm slightly worried about the beta status of the server-sent events, as that would mean the API might still change, which would not be good for the library. Maybe a beta version of the library is needed while the API is in beta, too. 🤔 So, a lot to consider here!

As a note to mainly myself, the docs for the server-sent events can be found here: https://developers.meethue.com/develop/hue-api-v2/core-concepts/#events

ZeroOne3010 avatar Jan 13 '22 11:01 ZeroOne3010

Hi,

I started to do some tests with the SSE API. It is pretty easy to use, but the identifiers are no longer compatible, as you already mentioned. I also found another incompatible change for the Friends Of Hue buttons. There is no push notification when pressing two buttons at once. The old API reported this as an extra button, which was nice.

I've used this dependency in my tests https://github.com/autopreet/apache-sseclient, but the byte-code is not compatible with Java 8.

philipparndt avatar Jan 13 '22 17:01 philipparndt

Luckily the client side of the SSE protocol seems simple enough, there should be no need to use any libraries... 🤞

ZeroOne3010 avatar Jan 14 '22 12:01 ZeroOne3010

Getting events from the server rather than polling is crucial. I'd suggest making breaking changes to support v2. People who want to continue using v1 can use the older version of this library.

NathanSweet avatar Feb 13 '22 18:02 NathanSweet

@philipparndt @NathanSweet Hi, I've just pushed a commit to the api-v2 branch in case you would like to take a look at the initial events API support. 😊 The new io.github.zeroone3010.yahueapi.v2.Hue (notice the "v2" in the package name there) class now has a subscribeToEvents(HueEventListener) method that you can call with your custom event listener that implements the simple interface there.

For some reason subscribing to the event stream breaks all other communication with the Bridge, for me at least. I've posted a question about it to the official Hue forum, but so far to no avail.

Also the subscribeToEvents method actually puts the thread to sleep after starting to listen for the events. Not as it's supposed to be, but I couldn't immediately figure out a way to properly return from the method while keeping the connection open! I've been using the HueEventsTestRun class for testing, and if I move the sleep there, no events are logged... 😕 (Give the test run class two arguments: IP address and API key, if you want to try it.) Any help would be appreciated. 😃

The events that the API returns should also, IMO, include a proper object reference to the resource that sent the event, e.g. a button, a light, or a sensor. Right now there's just the owner field which has an id and a type.

ZeroOne3010 avatar Jul 01 '22 13:07 ZeroOne3010

Hi @ZeroOne3010

Thanks for letting me know. I've switched to TypeScript in my project. I only implemented support for API v2 and it is up and running very stable 😄

Maybe interesting for you: I have the SSE API always running, and do not have issues with other applications that are using the API. Like iConnectHue for example.

philipparndt avatar Jul 06 '22 13:07 philipparndt

Just to close this old issue: version 3.0.0-alpha is now out and it supports those server sent events. :heart:

ZeroOne3010 avatar Apr 20 '23 06:04 ZeroOne3010