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

PUB/SUB Over websockets #163

Open
esatterwhite opened this issue Sep 19, 2016 · 7 comments
Open

PUB/SUB Over websockets #163

esatterwhite opened this issue Sep 19, 2016 · 7 comments
Labels

Comments

@esatterwhite
Copy link

I'm assuming this is a constraint of the nanomsg library, but is it possible to use the pub/sub sockets with a websocket transport? I was able to use the pair type as shown in the examples, but it won't connect using pub sub.

Does it only work with 2-way socket types?

@reqshark
Copy link
Collaborator

pub sub works, and if you're using it in a browser, verify mapping

@esatterwhite
Copy link
Author

Yeah, Im just running the ws example in this project. changed the client to sub.sp.nanomsg.org and the server to a pub socket type, The hand shake never completes and nothing will ever make it to the client. I put a send command in an interval and still nothing.

@esatterwhite
Copy link
Author

Also, how would you add a subscription in the browser ?

@reqshark
Copy link
Collaborator

reqshark commented Sep 20, 2016

client and server paradigm is not always helpful when doing nanomsg. better to talk about endpoints and socket types. we bind subscribers and just as we bind publishers... and likewise with connect and all other interchangeable socket types.

anyway let's take a look at your use case and see if we cant get something to work based on the example:

server.js:

const pub = require('nanomsg').socket('pub')
const sub = require('nanomsg').socket('sub')

pub.bind('ws://127.0.0.1:7790')
sub.bind('ws://127.0.0.1:7791')

/* nanomsg websocket text frames */
pub.wsopt('text')
sub.wsopt('text')

setInterval(()=> pub.send('hello from nanomsg @ '+new Date()), 500)


sub.on('data', function(msg){
  //pub.send(msg+'');
  console.log('msg recv\'d: '+msg);
});

require('http').createServer(function (req, res) {
  require('fs').createReadStream('index.html').pipe(res);
}).listen(3000);

index.html:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <p>nanomsg ws test:&nbsp;<span></span></p>
    <input type='text'>
    <button>send</button>
    <script>
      var ws = new WebSocket('ws://127.0.0.1:7790', [
      // see the rfc on sp websocket mapping:
      // raw.githubusercontent.com/nanomsg/nanomsg/master/rfc/sp-websocket-mapping-01.txt
        'pub.sp.nanomsg.org'
      ]);
      ws.onopen = function open() { }//do something
      ws.onerror = function error(err) { console.log(err); }
      ws.onmessage = function message(e) {
        document.querySelector('span').textContent = e.data
      }

      var sub = new WebSocket('ws://127.0.0.1:7791', [ 'sub.sp.nanomsg.org' ])
      sub.onclose = function eh(){}
      sub.onerror = function(){}
      sub.onopen = function(){}

      var input = document.querySelector('input');
      input.addEventListener('keydown', function(e){
        if (e.which === 13) {
          return send();
        }
      }, false);

      document
        .querySelector('button')
        .addEventListener('click', send, false);

      function send(e) {
        sub.send(input.value); // send input value
        input.value = '';     // clear input
      }

    </script>
  </body>
</html>

@esatterwhite
Copy link
Author

esatterwhite commented Sep 20, 2016

Ah, so the browser connection mapping needs to match the server. That wasn't really clear. I was using pub on the server and sub in the browser.

@esatterwhite
Copy link
Author

Is there a way to specify subscriptions on in the browser sub socket?

@reqshark
Copy link
Collaborator

Ah, so the browser connection mapping needs to match the server.

Exactly, nanosmg websocket options in the browser === not intuitive.

Is there a way to specify subscriptions on in the browser sub socket?

you mean like setting subscription channels? i don't think so, the issue came up once on libnanomsg, and from what i remember the answer is no

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

No branches or pull requests

2 participants