# [FileBufferReader.js](https://github.com/muaz-khan/FileBufferReader) / [Demo](https://www.WebRTC-Experiment.com/FileBufferReader/) / [Watch a YouTube video](https://www.youtube.com/watch?v=gv8xpdGdS4o) [](https://npmjs.org/package/fbr) [](https://npmjs.org/package/fbr) [](https://travis-ci.org/muaz-khan/FileBufferReader) All released versions: https://github.com/muaz-khan/FileBufferReader/releases Using FileBufferReader.js, you can: 1. Get list of array-buffers with each specific chunkSize 2. Chunks can be step-by-step shared with remote peers, or instantly shared using for-loop You can easily implement retransmission of chunks as well. You need to set `binaryType` to `arraybuffer`: ```javascript WebRTC_Data_Channel.binaryType = 'arraybuffer'; ``` ## A few points: 1. FileBufferReader itself doesn't do anything except reading the file(s) 2. You need to manually share chunks using your preferred medium or gateway 3. FileBufferReader currently uses memory to store chunks; which has storage limits. So, you may not be able to use FileBufferReader to read/share file with 1GB size or more. 4. FileBufferReader is added to support controlled-buffers transmissions whilst following Skype's file sharing style. It is MIT Licenced, which means that you can use it in any commercial/non-commercial product, free of cost. ```sh npm install fbr --production # or using bower bower install fbr ``` To use it: ```html ``` Or run localhost server: ```sh node server.js ``` Then open: `http://localhost:9001/` or `http://127.0.0.1:9001/`. ## [fbr-client](https://github.com/muaz-khan/FileBufferReader/tree/master/fbr-client) You can even try [socket.io file sharing client](https://github.com/muaz-khan/FileBufferReader/tree/master/fbr-client): ```sh npm install fbr-client ``` Then run the server: ```sh cd ./node_modules/fbr-client node server.js port=9001 ``` Then open: `http://localhost:9001/` or `http://127.0.0.1:9001/`. > You can modify development files from the `dev` directory; and use `grunt` tool to recompile into `FileBufferReader.js`. ## FileBufferReader API 1. `chunks` object. It contains multiple files' chunks. Even if you received chunks from remote peer, and invoked `addChunk` method; all chunks will be stored in same `chunks` object. `var fileChunks = fileBufferReader.chunks['file-uuid']`. 2. `readAsArrayBuffer` method. It reads entire file and stores chunkified buffers in `chunks` object. 3. `getNextChunk` method. It simply reads `last-position` and returns next available array-buffer chunk. 4. `onBegin`, `onEnd` and `onProgress` events. These are added only to support file progress bars. 5. `addChunk` method. It allows you store all received chunks in an array until entire file is received. 6. `convertToObject` method. FileBufferReader assumes that you're sending ArrayBuffer using WebRTC data channels. It means that you'll be getting ArrayBuffer type in the `onmessage` event. `convertToObject` method allows you convert ArrayBuffer into JavaScript object type, which is helpful to check type of message. 7. `convertToArrayBuffer` method. You can pass javascript object or any data-type, and this method will return `ArrayBuffer`. ## 1. Link The Library ``` https://cdn.webrtc-experiment.com/FileBufferReader.js # or https://cdn.rawgit.com/muaz-khan/FileBufferReader/master/FileBufferReader.js ``` ## 2. Select File (optional step) You can use `input[type=file].onchange` instead, which is **strongly recommended** over using `FileSelecter` because `FileSelector` object is incapable to handle failures or situations where browser doesn't fires `onchange` event. ```javascript var fileSelector = new FileSelector(); // *.png, *.jpeg, *.mp4, etc. fileSelector.accept = '*.*'; var btnSelectFile = document.getElementById('select-file'); btnSelectFile.onclick = function() { fileSelector.selectSingleFile(function(file) { // file == input[type=file] }); }; ``` ## 3. Read Buffers ```javascript var fileBufferReader = new FileBufferReader(); fileBufferReader.readAsArrayBuffer(file, function(fileUUID) { // var file = fileBufferReader.chunks[fileUUID]; // var listOfChunks = file.listOfChunks; // get first chunk, and send using WebRTC data channels // NEVER send chunks in loop; otherwise you'll face issues in slow networks // remote peer should notify if it is ready for next chunk fileBufferReader.getNextChunk(fileUUID, function(nextChunk, isLastChunk) { if(isLastChunk) { alert('File Successfully sent.'); } // sending using WebRTC data channels datachannel.send(nextChunk); }); }); ``` `readAsArrayBuffer` takes 3rd argument as well; where you can pass `chunkSize`, and your custom data. ```javascript var extra = { chunkSize: 15 * 1000, // Firefox' receiving limit is 16k userid: 'sender-userid' // MOST USEFUL object }; fileBufferReader.readAsArrayBuffer(file, callback, extra); ``` ## 4. When remote peer receives a chunk ```javascript datachannel.onmessage = function(event) { var chunk = event.data; if (chunk instanceof ArrayBuffer || chunk instanceof DataView) { // array buffers are passed using WebRTC data channels // need to convert data back into JavaScript objects fileBufferReader.convertToObject(chunk, function(object) { datachannel.onmessage({ data: object }); }); return; } // if you passed "extra-data", you can access it here: // chunk.extra.senderUserName or whatever else // if target peer requested next chunk if(chunk.readyForNextChunk) { fileBufferReader.getNextChunk(chunk.uuid, function(nextChunk, isLastChunk) { if(isLastChunk) { alert('File Successfully sent.'); } // sending using WebRTC data channels datachannel.send(nextChunk); }); return; } // if chunk is received fileBufferReader.addChunk(chunk, function(promptNextChunk) { // request next chunk datachannel.send(promptNextChunk); }); }; ``` ## 5. File progress helpers Link this script: ``` https://cdn.webrtc-experiment.com/FileProgressBarHandler.js # or https://cdn.rawgit.com/muaz-khan/FileBufferReader/master/fbr.0/dev/FileProgressBarHandler.js ``` Add a files-div: ```html
``` Add following code: ```javascript // this line is optional // however it allows you set the