socket.io based file stream
This package has three components
-
Only works on node environment because it's uses node
fs.creatReadStream
which makes sense for electron applications. -
Web is for browsers based app the component uses FileReader api to read file blob.
-
This component handlers all the request from both
Web
andClient
//Client
import { Client } from 'socket.io-file-stream'
const client = new Client(socket, {
filepath: '/path/to/file/music.mp3',
data: {
//you pass your own data here
name: 'music.mp3'
}
})
client
.upload('file-upload', data => {
console.log({ data })
})
.on('progress', c => {
console.log(c) //{total,size}
})
.on('done', data => {
console.log(data)
})
.on('pause', () => {
console.log('pause')
})
.on('cancel', () => {
console.log('canceled')
})
//Web
function onChange(inputElement) {
let file = inputElement.files[0]
const client = new Web(socket, {
file: file,
data: {
name: file.name
}
})
client
.upload('file-upload', data => {
console.log({ data })
})
.on('progress', c => {
console.log(c) //{total,size}
})
.on('done', data => {
console.log(data)
})
.on('pause', () => {
console.log('pause')
})
.on('cancel', () => {
console.log('canceled')
})
}
//Server
import { Server } from 'socket.io-file-stream'
io.on('connection', socket => {
console.log('hurrey')
const server = new Server(socket)
server.on('file-upload', ({ stream, data: { data } }, done) => {
console.log('stream')
const writable = createWriteStream(data.name, {
autoClose: true
})
stream.pipe(writable)
writable.on('close', () => {
//make sure to call this function
//only when you're done, you can
//pass a value to it which will be
//sent back to client as well
done('good')
})
})
})
Please check the example folder for clues on how to use the package until I'm able to document this package well