You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@ryanaltair/socket.io-stream

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ryanaltair/socket.io-stream

Upload file as stream with socket.io, fork from @Akumzy/socket.io-stream

0.2.2
latest
Source
npmnpm
Version published
Weekly downloads
8
700%
Maintainers
1
Weekly downloads
 
Created
Source

socket.io-stream

socket.io based file stream

This package has three components

Client:

Only works in Nodejs environment like in ElectronJs.

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 '@ryanaltair/socket.io-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 '@ryanaltair/socket.io-stream'
import { appendFileSync } from 'fs'
const io = require('socket.io')(8090)

io.on('connection', socket => {
  const server = new Server(socket)

  server.on('file-upload', async ({ stream, data, ready }, ack) => {
    try {
      await someTask()
      ready()
      stream.subscribe({
        next({ buffer, flag }) {
          appendFileSync(data.name, buffer, {
            encoding: 'binary',
            flag
          })
        },
        complete() {
          ack(null, [1, 2, 3])
        },
        error(err) {
          ack(err)
          console.error(err)
        }
      })
    } catch (err) {
      console.error(err)
    }
  })
})

Keywords

electron

FAQs

Package last updated on 24 Sep 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts