Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add websocket endpoint #23

Open
patrickelectric opened this issue Nov 13, 2023 · 5 comments
Open

Add websocket endpoint #23

patrickelectric opened this issue Nov 13, 2023 · 5 comments

Comments

@patrickelectric
Copy link

Describe the feature

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

@gabrik
Copy link
Contributor

gabrik commented Nov 14, 2023

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
Copy link
Author

patrickelectric commented Nov 14, 2023

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.

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
Copy link
Contributor

gabrik commented Nov 14, 2023

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
Copy link
Author

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

@patrickelectric
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants