Akumzy / socket.io-file-stream

socket.io based file stream for both browsers or node environments (Electron, nw.js)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

socket.io-file-stream

socket.io based file stream

This package has three components

  • Client:

    Only works on node environment because it's uses node fs.creatReadStream which makes sense for electron applications.

  • Web:

    Web is for browsers based app the component uses FileReader api to read file blob.

  • Server:

    This component handlers all the request from both Web and Client

//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

About

socket.io based file stream for both browsers or node environments (Electron, nw.js)

License:MIT License


Languages

Language:TypeScript 76.5%Language:JavaScript 14.0%Language:HTML 9.5%