Node.js BinaryServer: send message to client at end of stream?

I am using node.js BinaryServer to stream binary data, and I want a callback event from the server after the client calls the function .Stream.end().

I can’t understand how I can send a message or some kind of notification when the node.js server really closes the connection to the stream?

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
        });
    });

});

JS client:

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

On the server side, you can send streams to the client using .send. You can send various types of data, but in this case there may be a fairly simple string.

'stream' .

Node JS:

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

JS:

client = new BinaryClient(nodeURL);
client.on('stream', data => {
    console.log(data); // do something with data
});
window.Stream = client.createStream({ metaData });
....
window.Stream.end();
+1

Source: https://habr.com/ru/post/1673811/


All Articles