socket.io-pull-stream
v0.1.5
Published
Pull Streams for socket.io
Downloads
308
Readme
socket.io-pull-stream
Pull Streams for socket.io
API
sp(io[, opt])
Will add the functions below to
io
io
: Socket.io-client instanceopt
: Default options for every stream
.createSink([id, opt])
Will create a sink with
id
. If no id is provided a new id will be generated and can be found undersink.id
opt
: Object. Config for streamcodec
: Codec to use.Either string (
hex, plain, buffer
) or object ({encode: data => ..., decode: data => ...}
).Default:
plain
Returns: Sink Function
.createSource(id[, opt])
Will create a source that reads from a sink with id
id
on the other side.opt
: Object. Config for streamcodec
: Codec to use.Either string (
hex, plain, buffer
) or object ({encode: data => ..., decode: data => ...}
).Default:
plain
Returns: Source Function
.createProxy(id, to)
Will proxy the stream
id
to socketto
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