Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

node.js - How to stream data over socket.io to client

I have socket.io sending a basic object from server to client. This bit works fine.

Now want to send a stream from server to client, using event-stream (specifically the results of a block-chain query). I am getting unexpected results in the browser console..

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

/* This sends results to stdout, fine
io.on('connection', function(socket){
    stream.pipe(es.map(function(block, callback) {
        callback(null, util.inspect(block) + '
') 
    })).pipe(process.stdout);

    // And this sends a simple object to the client 
    socket.emit('blockchainOps', {"Foo!":"Doo!"} );
});
*/

// Putting both together sends strange connection data to client
io.on('connection', function(socket){

        socket.emit('blockchainOps', function() {

            stream.pipe(es.map(function(block, callback) {
                callback(null, util.inspect(block) + '
');
            }))
        })

    });

What I get in the client console appears to be some kind of TCP socket function,

? (){if(!n){n=!0;var r=a(arguments);u("sending ack %j",r),e.packet({type:i.ACK,id:t,data:r})}}

Can anyone help me understand what's going on and what I'm doing wrong?

== EDIT UPDATE ==

As suggested in comments, I've tried socket.io-stream to augment event-stream.

var es = require('event-stream') 
var util = require('util')
var ss = require('socket.io-stream'); 
var stream = ss.createStream();

io.on('connection', function(socket){

ss(socket).emit('blockchainOps', stream, function(){


        client.blockchain.getBlockStream()
            .pipe(es.map(function(block, callback) {
                callback(null, util.inspect(block) + '
') 
                }))
            .pipe(process.stdout)


}());

});

This time I get a socket object returned in the browser console which does not seem to be the stream data I was hoping for.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If anyone is looking for a working socket.io stream example

// server side
const { pipeline } = require('stream')
const server = require('http').Server().listen(8080)
const io = require('socket.io')(server)
const ss = require('socket.io-stream')

io.on('connection', (socket) => ss(socket).on('stream', (stream) => {
  pipeline(stream, process.stdout,  (err) => err && console.log(err))
}));


// client side
const client = require('socket.io-client')
const socket = client.connect('http://localhost:8080')

socket.on('connect', () => {
  const stream = ss.createStream()
  ss(socket).emit('stream', stream)
  pipeline(process.stdin, stream,  (err) => err && console.log(err))
});


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...