Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io-Stream not sending to Client

I'm am trying to send (relay) a continuous stream of utf-8 data from server to client. While I can eyeball the data arriving on the server, I cannot pipe it into the Socket and forward it to the Client.

Nodejs Server,

var io = require('socket.io')(server);
app.io = io;
var dsteem = require('dsteem')
var es = require('event-stream') 
var client = new dsteem.Client('https://api.steemit.com')
var ss = require('socket.io-stream'); 
var outBoundStream = ss.createStream();
var stream = client.blockchain.getBlockStream();

io.on('connection', function(socket) {
    /* Eyeball the data in the nodejs console */ 
    stream.pipe(es.map(function(block, callback) {
    callback(null, util.inspect(block, {colors: true, depth: null}) + '\n')
    })).pipe(process.stdout);
    /* Send stream data to client */
    ss(socket).on('ready', function(){
        console.log('Here it comes...');
        ss(socket).emit('sending', function(){
            stream.pipe(es.map(function(block, callback) {
            callback(null, util.inspect(block, {colors: true, depth: null}) + '\n')
            }));
        });
    });

    ss(socket).on('disconnect', function(){
        console.log('Disconnected');
    });
});

The data from .getBlockStream() that I can see in the nodejs console looks like something like this snip,

 {['1b3c0394d1146cecdc0b5d38486f0b99a6d7c750',
 '85e3110930f87510b4782d50de86180ecbacf05d',
 '7cbd01b2a9d515736860014eabbc18893707e0c4',
 '307308574fec6336493d0a9713ee98da21bbc1a7',
 '9cb1636cdb2591361b7d7e4c9d04096c1bc93aff',
 '27d22c51a6f3e0d764039b095046ec563e53ae6b',
 '153451e251816823e3342d2f0b1425570d0f3d36',
 '0a17b03e9fddfde49ad632dfe2437d76321c34eb',
 'fdd23d78865d1855b16d171d24199dd4e2fba83a',
 '306e6f2b11e6bd6e2ef37acbe6e593ef2d1c0e1e' ] }

Client,

<script src="/socket.io/socket.io.js"></script>
<script src="/javascripts/socket.io-stream.js"></script>
<script> 
$(function () {
    var socket = io();
    ss(socket).emit('ready', 'Client ready, send me a stream...');
    ss(socket).on('sending', function(stream, d){
            console.log(stream);
  });
});
</script>

In the browser console I'm only seeing this function,

ƒ () {
        var args = slice.call(arguments);
        args = self.encoder.encode(args);
        ack.apply(this, args);
      }

Can anyone help me understand what I'm doing wrong?

like image 948
Colin Avatar asked Feb 06 '18 15:02

Colin


People also ask

Can I use Socket.IO for video streaming?

In this tutorial, you'll learn how to create a WebSocket service which can be eligible to handle video streaming. To do this, we'll use ExpressJS and Socket.io.

Does Socket.IO use long polling?

js) and the Socket.IO client (browser, Node. js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.

What is the difference between Socket.IO and socket IO client?

socket-io. client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).


1 Answers

The issue happens because you have below code

ss(socket).emit('sending', function(){
   stream.pipe(es.map(function(block, callback) {
   callback(null, util.inspect(block, {colors: true, depth: null}) + '\n')
}))

The second parameter of emit method expects data and your are sending it a function and hence what you receive on client side is a blank function

The fix is simple, just send the data

io.on('connection', function(socket) {
    /* Eyeball the data in the nodejs console */
    /* Send stream data to client */
    ss(socket).on('ready', function(){
        console.log('Here it comes...');
        stream.pipe(es.map(function(block, callback) {
            ss(socket).emit('sending', block);
            callback(null, util.inspect(block, {colors: true, depth: null}) + '\n')

        })).pipe(process.stdout);

    });

    ss(socket).on('disconnect', function(){
        console.log('Disconnected');
    });
});

Once that is done, you can see the data on client side

data client

like image 185
Tarun Lalwani Avatar answered Oct 15 '22 19:10

Tarun Lalwani