mkg20001 / socket.io-pull-stream

Pull Streams for socket.io
2 stars 1 forks source link

socket.io-pull-stream

Pull Streams for socket.io

API

Examples

File Stream

Server:

const sp = require('socket.io-pull-stream')
const fs = require('fs')
const toPull = require('stream-to-pull-stream')
const pull = require('pull-stream')

io.on('connection', client => {
  sp(client)
  const ws = toPull.source(fs.createReadStream('some_file.txt'))
  const sink = client.createSink()
  client.emit('file', sink.id) // to send the stream just emit the id (IMPORTANT: EMIT THE ID FIRST, LATER CREATE THE SOURCE IN SYNC)
  pull(
    ws,
    sink
  )
})

Client:

const sp = require('socket.io-pull-stream')
const pull = require('pull-stream')

sp(io)

io.on('file', id => {
  pull(
    io.createSource(id), // to recieve just create a source with the id (IMPORTANT: DO THAT SYNC)
    pull.collect((err, data) => {
      if (err) throw err
      console.log(Buffer.from(data).toString()) // our data
    })
  )
})

To send streams between clients the stream need to be proxied

// TODO :add