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

Cannot make simple surveyor/respondent example to work #158

Open
janflyborg opened this issue Jun 21, 2016 · 3 comments
Open

Cannot make simple surveyor/respondent example to work #158

janflyborg opened this issue Jun 21, 2016 · 3 comments
Labels

Comments

@janflyborg
Copy link

Trying to make a simple surveyor/respondent example with the following code, but the surveyor fails after the first survey has been completed. As usual I might doing something completely wrong, since I am new to this library. Any ideas?

This is my surveyor:

var nano = require('nanomsg');
var surv = nano.socket('surveyor');

surv.bind('tcp://127.0.0.1:7789');

surv.on('data', function (str) {
    console.log('Receiving');
    console.log(String(str));
});

setInterval(function() {
    console.log('Sending');
    surv.send('Hello');
}, 4000);

And this is my respondent:

var nano = require('nanomsg');
var resp = nano.socket('respondent');

resp.connect('tcp://127.0.0.1:7789');

resp.on('data', function (str) {
    console.log(String(str));
    resp.send('Answer');
});

And the output from the surveyor looks like this:

Sending
Receiving
Answer
Sending
events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: stream.push() after EOF
    at readableAddChunk (_stream_readable.js:152:17)
    at Socket.Readable.push (_stream_readable.js:130:10)
    at Socket.<anonymous> (/home/janf/nano_msg/node_modules/nanomsg/lib/index.js:209:24)


@reqshark reqshark added the bug label Jun 21, 2016
@reqshark
Copy link
Collaborator

reqshark commented Jun 21, 2016

yea that's right, surveyor/respondent doesn't work as expected. The fix is non-trivial

As a work-around, close and reopen your surveyor socket over the duration of your survey intervals:

// respondent socket
const res = require('nanomsg').socket('respondent')
res.connect('tcp://127.0.0.1:12345')
res.on('data', ()=> res.send('res sock is online'))

setInterval(()=> {

  var replies = 0

  // surveyor socket
  const sur = require('nanomsg').socket('surveyor', { encoding:'utf8' })
  sur.bind('tcp://127.0.0.1:12345')
  setTimeout(()=> sur.send('sup'), 100)
  setTimeout(()=> sur.send('sup'), 200)
  setTimeout(()=> sur.send('sup'), 300)
  sur.on('data', (msg)=>{
    console.log(msg)
    if(++replies > 2)
      sur.close()
  })
}, 4000)

@janflyborg
Copy link
Author

janflyborg commented Jun 21, 2016

OK I see. Thanks for the help.

Is this a problem with nanomsg itself or is it just this binding? Are these quirks documented somewhere? I've tried two things with nanomsg so far and both of them has lead to a new bug report, which makes me wonder if I can use this for production or if I should stick with 0MQ?

@reqshark
Copy link
Collaborator

no problem, and thanks for raising good issues!

All these protocols are best written in C.

If you're using JavaScript, however, I've found PUB/SUB and REQ/REP are fit for production, but SURVEY/RESPONDENT is a bit temperamental.

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