eclipse-zenoh / zenoh-plugin-webserver

A zenoh plug-in implementing an HTTP server mapping URLs to zenoh paths. This plugin can be used to set-up a Web server where the resources are retrieved from geo-distributed zenoh storages, each leveraging various backends (file system, database, memory...)
Other
14 stars 12 forks source link

Add websocket endpoint #23

Open patrickelectric opened 8 months ago

patrickelectric commented 8 months ago

Describe the feature

It would be nice for web services to subscribe to topics using websocket and fetching data in real time

gabrik commented 8 months ago

Hi @patrickelectric, Zenoh actually has a WebSocket transport: https://github.com/eclipse-zenoh/zenoh/tree/master/io/zenoh-links/zenoh-link-ws

In order to use it you need to start a router with the WebSocket locator configured: e.g., zenohd -l ws/127.0.0.1:7887 Then Zenoh will listen for WebSocket connections.

Furthermore, we are in the process of develop a JavaScript/TypeScript API that will leverage the WebSocket transport to communicate to Zenoh routers and peers and thus allow to fetch data in realtime.

patrickelectric commented 8 months ago

Hi @gabrik, and thanks for the info.

Now I have more questions :smile:

How can I use this websocket ? It appears that when I try to send anything on it the server breaks my connection without tell me why.

If I let it open I do not receive any messages from it as well, I'm using the following code:

let socket = new WebSocket("ws://0.0.0.0:8002");

socket.onopen = function (e) {
  console.log("[open] Connection established");
  console.log("Sending to server");
  // Help, I need somebody, help, not just anybody!
  // socket.send("/**");
};

socket.onmessage = function (event) {
  console.log(`[message] Data received from server: ${JSON.stringify(event)}`);
};

socket.onclose = function (event) {
  if (event.wasClean) {
    console.log(
      `[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`
    );
    return;
  }

  console.error("[close] Connection died");
};

socket.onerror = function (error) {
  console.error(`[error] ${JSON.stringify(error)}`);
};

setTimeout(function () {
  socket.close();
  console.log("Closing the WebSocket connection after 60 seconds");
}, 60000);
gabrik commented 8 months ago

Hi @gabrik, and thanks for the info.

Now I have more questions 😄

How can I use this websocket ? It appears that when I try to send anything on it the server breaks my connection without tell me why.

The WebSocket Transport for Zenoh expects the Zenoh protocol to be used as the payload of the WebSocket. That's why I said that we are working on a JS/TS API that is able to leverage this WebSocket transport to expose Zenoh's data to web applications.

The WebSocket transport can be easily used by the Zenoh examples, but so far not from JS (unless you implement the whole protocol stack, or wait for an initial version of our JS API to be available).

patrickelectric commented 8 months ago

Thanks for the update @gabrik, where is this being worked on at the moment ? I may help on that.

patrickelectric commented 1 month ago

This is being worked here: https://github.com/ZettaScaleLabs/zenoh-ts