[SOLVED] OpenHAB2 RestAPI and Web Sockets

Hello,

I try to learn to use OpenHAB2 Rest API and Web Sockets.

I am able to get my sitemap throught the rest api with:

GET /rest/sitemaps/{name}

Now I want to estalish a connection with web sockets to get changes of items. I tried to do so with normal JavaScript WebSocket class:

var socket  = new WebSocket('ws://{ip}:8080/rest/items/{itemName}');

But all I get is:

WebSocket connection to 'ws://{ip}:8080/rest/items/{itemName}' failed: Error during WebSocket handshake: Unexpected response code: 200

What is wrong with this?

Thank you!

Because it is NOT a websocket server!
The “rest” in the URL should be enough of a hint.

Use EventSource(…/rest/events) instead.

Thank you I got this working!

I wonder for what POST /sitemaps/events/subscribe is.

It is to get a subscription ID that you can use later to listen for updates in sitemap through a SSE connection.