Git Product home page Git Product logo

peer-file's Introduction

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

peer-file's People

Contributors

michaelrhodes avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.