One-to-One File Sharing using RTCMultiConnection
Copyright © 2013 Muaz Khan<@muazkh>.
Open Data Channel
or join:
Share Files |
Getting started with RTCMultiConnection
<script src="https://www.webrtc-experiment.com/RTCMultiConnection-v1.1.js"></script> <script> var connection = new RTCMultiConnection(); // for One-to-One file sharing: set direction = 'one-to-one' connection.direction = 'one-to-one'; // and set session = 'only-data' connection.session = 'data'; // to create/open a new session connection.open('session-id'); // if someone already created a session; to join it: use "connect" method connection.connect('session-id'); // to send a file document.querySelector('input[type=file]').onchange = function () { var file = this.files[0]; connection.send(file); }; </script>Remember, A-to-Z, everything is optional! You can set session-id in constructor or in open/connect methods. It is your choice!
Features:
- Share file directly — of any size
- Share text-message of any length
- Share data directly
Additional:
<script> // to be alerted on data ports get open connection.onopen = function(channel) {} // to be alerted on data ports get new message connection.onmessage = function(message) {} // by default; connection is [many-to-many]; you can use following directions connection.direction = 'one-to-one'; connection.direction = 'one-to-many'; connection.direction = 'many-to-many'; // --- it is default // show progress bar! connection.onFileProgress = function (packets) { // packets.remaining // packets.sent // packets.received // packets.length }; // on file successfully sent connection.onFileSent = function (file) { // file.name // file.size }; // on file successfully received connection.onFileReceived = function (fileName) {}; </script>
Errors Handling
<script> // error to open data ports connection.onerror = function(event) {} // data ports suddenly dropped connection.onclose = function(event) {} </script>
Use your own socket.io for signaling
<script> // by default Firebase is used for signaling; you can override it connection.openSignalingChannel = function(config) { var socket = io.connect('http://your-site:8888'); socket.channel = config.channel || this.channel || 'default-channel'; socket.on('message', config.onmessage); socket.send = function (data) { socket.emit('message', data); }; if (config.onopen) setTimeout(config.onopen, 1); return socket; } </script>