Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

michaelrhodes/peer-file

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

peer-file

peer-file is a little library for doing p2p file transfers over webrtc.

Browser support

Install

$ npm install peer-file 

Usage

<script src="/lib/peer.js"></script>
<script src="build.js"></script>
var send = require('peer-file/send')
var receive = require('peer-file/receive')
var peer = new Peer('some-id')

peer.on('connection', function(connection) {
  connection.on('open', function() {

    // Receive
    receive(connection)
      .on('incoming', function(file) {
        this.accept(file) || this.reject(file)
      })
      .on('progress', function(file, bytesReceived) {
        Math.ceil(bytesReceived / file.size * 100)
      })
      .on('complete', function(file) {
        new Blob(file.data, { type: file.type })
      })

    // Send
    var file = input.files[0]
    send(connection, file)
      .on('progress', function(bytesSent) {
        Math.ceil(bytesSent / file.size * 100)
      })
  })
})

peer-file was developed using peerjs, however it isn’t bound to that library. So long as the provided connection object emits data events, has a send method, and can handle JSON, it could be substituted.

Complete API

// Single use
peerfile.send(connection, file)
  .on('accept', function() {})
  .on('reject', function() {})
  .on('progress', function(bytesSent) {})
  .on('complete', function() {})
  .on('cancel', function() {})
  .pause()
  .resume()
  .cancel()

// Long running
peerfile.receive(connection)
  .on('incoming', function(file) {})
  .on('progress', function(file, bytesReceived) {})
  .on('complete', function(file) {})
  .on('cancel', function(file) {})
  .accept(file)
  .reject(file)
  .pause(file)
  .resume(file)
  .cancel(file)

License

MIT

About

A little library for doing p2p file transfers over webrtc.

Resources

Stars

Watchers

Forks

Packages

No packages published