Wednesday, April 26, 2017

Node.js BinaryServer: Send a message to the client on stream end?

Leave a Comment

I'm using a node.js BinaryServer for streaming binary data and I want a callback event from the server, after the client calls for the .Stream.end() function.

I can't seem to understand - How can I send a message or some kind of notification when the node.js server actually closes the stream connection ?

Node JS:

server.on('connection', function(client) {      client.on('stream', function (stream, meta) {          stream.on('end', function () {             fileWriter.end();             // <--- I want to send an event to the client here         });     });  }); 

client JS:

client = new BinaryClient(nodeURL); window.Stream = client.createStream({ metaData }); .... window.Stream.end(); //  <--- I want to recieve the callback message 

1 Answers

Answers 1

On the server side, you can send streams to the client with .send. You can send a variety of data types, but a simple string will probably suffice in this case.

On the client side you can also listen to the 'stream' event to receive data back from the server.

Node JS:

server.on('connection', function(client) {     client.on('stream', function (stream, meta) {         stream.on('end', function () {             fileWriter.end();             client.send('finished');         });     });     }); 

client JS:

client = new BinaryClient(nodeURL); client.on('stream', data => {     console.log(data); // do something with data }); window.Stream = client.createStream({ metaData }); .... window.Stream.end(); 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment