Using websockets in bindings

Continuing the discussion from openHAB2 roadmap:

Hi Pascal,

If I get you right, what you are asking for is actually the other direction: You want to be a client to some device; this is completely unrelated to the REST API on the openHAB runtime.
For this, you can use a websocket client library (such as from Jetty). This is not yet available in the core runtime, but it could be added, if required.

Regards,
Kai

Hi Kai,

No, I really need to be a server in openHAB like atmosphere provided it before. The client will be the android devices.
What do you think that I’m asking is for the other direction?

Regards,

Pascal

Because you said “interface to an android device from a binding”.
Why would you need a websocket connection from Android to the runtime? What is the use case?

In fact, a voice recognition application is running on the android device. We need to transmit the result of recognition process to a binding or a service in openhab and in the other side, transmit a text from this binding to the android device to speech it.

The binding manages the android devices and create an instance of a thing for each of it.

Regards,

Pascal

Not sure why you would need websockets for that. Posting stuff to the server from the app can be done through regular HTTP POST. And for sending infos to the app, we have SSE in place over which you can send your own events as well.

Hi Kai,

In fact, in some cases the android device will be outside with unknown IP address in openhab point of vue then we can’t use HTTP post to send data to it.
But the device know the IP address of openHAB platform then with creating websocket connexion by android device it can send and receive notifications.

Yes, Kai, I had a look SSE and we will investigate this way. It seems to be a solution for us.

Regards,

Pascal

I was talking to send stuff from the app to openHAB through HTTP POST - this is always perfectly possible as your app only needs to know the IP of openHAB and not the other way round. SSE is the other direction, i.e. server push.

Yes, I agree with this method : HTTP post from the app to OpenHab and SSE in the other direction.
We will test that in few days.

Another question is the cross-domain-origin : we need to accept request from another domains. How can I configure that in openHAB ?

Regards,

Pascal

This should work by adding

com.eclipsesource.jaxrs.connector:cors=enable

to your conf/services/runtime.cfg file.

Thanks Kai

I understand in Eclipse Smarthome documentation that for using SSE to receive notifications of a specific item, I have to listen to /rest/events?topics=/smarthome/items/. Is that right?

In this case where I can find the description of received JSON object (or where I can look in the code?)

Regards,

Pascal

Read here everything you need to know about events.

Hi Kai,

I’ve added the com.eclipsesource.jaxrs.connector:cors=enable but I have an error 404 send to my server about the allowed origin.
Does this adding is enough to allow origin of all ?

Regards,

Pascal

I have no clue.
@Stratehm is the expert on CORS, so maybe he can help?

Hi,

The CORS filter does not change the HTTP status code of responses. If you receive a 404 error, the URL of your request may not be correct. I do not think it is a CORS problem.

Sorry for the delay,
It works now. Effectivelly, It wasn’t a CORS problem but a wrong URL.

Thanks.

We use now the SSE server push to be notified of state changed. It works fine.
However, the connection seems to be broken after 1 or 2 minutes and no events will be notified after that.

Also, the javascript client doesn’t see any event according to the changing of the readyState connection.

We’ve tested the client also with a nodeJS server and SSE to compare. Apparantly when we stop the server, we’ve got an error and the readyState changes to state 2 (closed).

How can SSE push server can notify the closing of connection to the client ?
How does the mecanism reconnects automaticaly ?

Regards,
Pascal