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

How to use zeromq.js in HTML page #264

Closed
jay11ca39 opened this issue Jul 6, 2018 · 6 comments
Closed

How to use zeromq.js in HTML page #264

jay11ca39 opened this issue Jul 6, 2018 · 6 comments

Comments

@jay11ca39
Copy link

Hello All,

It is not an issue, It is a query.
I am trying to create a web page [HTML] which will be creating the zeroMQ subscriber and will subscribe to a particular publisher. and will update the web page as and when it receive data from publisher.

Following is my code:

<html>
   <head>
      <title>Trying</title>
   </head>
   <body>
      <button type="button" id="button1">Start Subscriber!</button>
      <hr>
      <textarea style="width:500px;height: 400px" id="output"></textarea>

      <script> 
         var output = document.getElementById("output");
         var input = document.getElementById("button1");
         input.addEventListener("click", function () {
              var zmq = require('zeromq');
              var sock = zmq.socket('sub');
              sock.connect('tcp://127.0.0.1:3000');
              sock.subscribe('kitty cats');
              output.textContent = 'Actual subscriber connected to port 3000'  ---> it is not getting printing.
         });
        //callback     
        sock.on('message', function(topic, message) {
              var output = document.getElementById("output");
	      console.log('received a message related to:', topic.toString(), 'containing message:', message.toString());
              output.textContent = message.toString()
        }); 
      </script>

   </body>
</html>

As I am new to this. I am not sure whether i am going in right direction.
Any help will be appreciable..

@rgbkrk
Copy link
Member

rgbkrk commented Jul 6, 2018

You have to write a server side endpoint that will translate it to websockets, zeromq is a raw socket (TCP primarily). zeromq can not run directly in a web browser (unless it's in something like electron).

@ronkorving
Copy link

If you want something that will run in the browser, you're probably looking for something built on top of WebSockets. For example, https://www.npmjs.com/package/socket.io

@xemasiv
Copy link

xemasiv commented Jul 9, 2018

You can also use plain WebSockets on the client-side and uWebSockets on the server-side, in-case you find socket.io quite bulky.

WebSockets @ MDN: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
uWebSockets @ Github: https://github.com/uNetworking/uWebSockets
uWebSockets NodeJS bindings: https://github.com/uNetworking/uWebSockets-bindings/tree/master/nodejs

@yoelk
Copy link

yoelk commented Mar 25, 2019

@rgbkrk Can ZMQ be run directly on Electron? How come it can be done there and not in a browser?
Could you please share a link to information about how to ZMQ over electron? Thanks a lot!

@filips123
Copy link

@yoelk

Yes, it can. But you need to rebuild it as described in README file.

The reason it works in Electron is that TCP sockets are available in Node.js and Electron, but not in client-side JavaScript in the browser.

@rolftimmermans
Copy link
Member

To add the the previous replies, there is support for WebSockets transport in progress. When it becomes available it will be supported in this library for server-side use. Usage in a browser will require JS-based client.

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

7 participants