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

Examples of use as a stream? #6

Closed
giacecco opened this issue Aug 1, 2014 · 2 comments
Closed

Examples of use as a stream? #6

giacecco opened this issue Aug 1, 2014 · 2 comments

Comments

@giacecco
Copy link

giacecco commented Aug 1, 2014

Hi gdaws,
Can you offer an example of using Stompit as a stream, if it is possible? I would like to understand if it is feasible to do something like in the example below:

var writeStream = fs.createWriteStream('./path/to/file.ext');
stompit.connect(CONNECTION_PARAMETERS, function (err, client) {
    client.subscribe(SUBSCRIPTION_PARAMETERS).pipe(writeStream);
});

Thanks,

G.

@gdaws
Copy link
Owner

gdaws commented Aug 3, 2014

Here's an example of how to write messages to a file:

var writeStream = fs.createWriteStream('./path/to/file.ext');

stompit.connect(CONNECTION_PARAMETERS, function (err, client) {

  client.subscribe(SUBSCRIPTION_PARAMETERS, function onMessage(err, message) {

    message.on('end', function() {
      message.ack();
    });

    message.pipe(writeStream, {end: false});
  });
});

The onMessage function is called for each incoming message. The call for the next message is made after the end event of the current message.

The end: false option on the pipe call keeps the write stream open for the next message.

@giacecco
Copy link
Author

Thanks!

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