DataChannel.js : A JavaScript wrapper library for RTCDataChannel APIs / Demos
npm install datachannel
# or
bower install datachannel
DataChannel.js is a JavaScript library useful to write many-to-many i.e. group file/data sharing or text chat applications. Its syntax is easier to use and understand. It highly simplifies complex tasks like any or all user rejection/ejection; direct messages delivery; and more.
If you want all DataChannel.js functionalities along with media streaming and runtime additions/deletions then RTCMultiConnection.js is a good chose with similar APIs/syntax.
Features
- Direct messages � to any user using his
user-id
- Eject/Reject any user � using his
user-id
- Leave any room (i.e. data session) or close entire session using
leave
method - File size is limitless!
- Text message length is limitless!
- Size of data is also limitless!
- Fallback to firebase/socket.io/websockets/etc.
- Users' presence detection using
onleave
- Latency detection
- Multi-longest strings/files concurrent
- File queue support added. Previously shared files will be auto transmitted to each new peer.
Demos using DataChannel.js
- DataChannel basic demo
- Auto Session Establishment and Users presence detection
- Text Chat using Pusher and DataChannel.js
Try a Quick Demo (Text Chat)
Open NEW DataChannel
First Step: Link the library
Last Step: Start using it!
var channel = '[optional] channel-name';channel;
open/connect data channels
// to create/open a new channelchannel; // if someone already created a channel; to join it: use "connect" methodchannel;
onopen
and onmessage
If you're familiar with WebSockets; these two methods works in the exact same way:
channel { }channel { }
user-ids
can be used to send direct messages or to eject/leave any user:
ondatachannel
Allows you show list of all available data channels to the user; and let him choose which one to join:
channel { channel; // or channel; // id: unique identifier for the session // owner: unique identifier for the session initiator};
Use custom user-ids
channeluserid = 'predefined-userid';
Remember; custom defined user-id
must be unique username.
Direct messages
channelchannelsuserid;
user-ids
Eject/Reject users using their channel; // throw a user out of your room!
Close/Leave the entire room
channel; // close your own entire data session
Detect users' presence
channel { };
Auto Close Entire Session
When room initiator leaves; you can enforce auto-closing of the entire session. By default: it is false
:
channelautoCloseEntireSession = true;
It means that session will be kept active all the time; even if initiator leaves the session.
You can set autoCloseEntireSession
before calling leave
method; which will enforce closing of the entire session:
channelautoCloseEntireSession = true;channel; // closing entire session
uuid
for files
You can get uuid
for each file (being sent) like this:
channel;var uuid = fileuuid; // "file"-Dot-uuid
To Share files
var progressHelper = {}; // to make sure file-saver dialog is not invoked.channelautoSaveToDisk = false; channel { var helper = progressHelperchunkuuid; helperprogressvalue = chunkcurrentPosition || chunkmaxChunks || helperprogressmax; ;}; channel { var div = document; divtitle = filename; divinnerHTML = '<label>0%</label> <progress></progress>'; ; progressHelperfileuuid = div: div progress: div label: div ; progressHelperfileuuidprogressmax = filemaxChunks;}; channel { progressHelperfileuuiddivinnerHTML = '<a href="' + fileurl + '" target="_blank" download="' + filename + '">' + filename + '</a>';}; channel { progressHelperfileuuiddivinnerHTML = '<a href="' + fileurl + '" target="_blank" download="' + filename + '">' + filename + '</a>';}; { if progressposition == -1 return; var position = +progressposition1 || 100; labelinnerHTML = position + '%';}
File Queue
File Queue support added to make sure newly connected users gets all previously shared files.
You can see list of previously shared files:
console;
Auto-Save file to Disk
By default; autoSaveToDisk
is set to true
. When it is true
; it will save file to disk as soon as it is received. To prevent auto-saving feature; just set it false
:
channelautoSaveToDisk = false; // prevent auto-saving!channel { // file.url // file.uuid hyperlinkhref = fileurl;};
Latency Detection
channel { console;};
Concurrent Transmission
You can send multiple files concurrently; or multiple longer text messages:
// individuallychannel;channel;channel; // or as an arraychannel; channel;channel;channel;
Errors Handling
// error to open data portschannel {} // data ports suddenly droppedchannel {}
Data session direction
Default direction is many-to-many
.
channeldirection = 'one-to-one';channeldirection = 'one-to-many';channeldirection = 'many-to-many';
=
For signaling; please check following page:
https://github.com/muaz-khan/WebRTC-Experiment/blob/master/Signaling.md
Remember, you can use any signaling implementation that exists out there without modifying any single line! Just skip below code and open above link!
Resources
- Video Presentation for
openSignalingChannel
: https://vimeo.com/91780227 - Documentation for
openSignalingChannel
: http://www.rtcmulticonnection.org/docs/openSignalingChannel/
your own socket.io for signaling
UsedataChannel { var channel = configchannel || thischannel || 'default-channel'; var socket = io; socketchannel = channel; socket; socket { socket; }; socket;};
Use Pusher for signaling
A demo & tutorial available here: http://pusher.com/tutorials/webrtc_chat
Another link: http://www.rtcmulticonnection.org/docs/openSignalingChannel/#pusher-signaling
firebase for signaling
Use// firebase stores data on their servers// that's why transmitting room once// unlike other signalling gateways; that// doesn't stores data on servers.channeltransmitRoomOnce = true; channel { channel = configchannel || thischannel || 'default-channel'; var socket = 'https://chat.firebaseIO.com/' + channel; socketchannel = channel; socket; socket { this; }; socket; thissocket = socket; return socket;};
Other Signaling resources
- XHR for Signaling
- WebSync for Signaling
- SignalR for Signaling
- Pusher for Signaling
- Firebase for Signaling
- PubNub for Signaling
transmitRoomOnce
transmitRoomOnce
is preferred when using Firebase for signaling. It saves bandwidth and asks DataChannel.js library to not repeatedly transmit room details.
channeltransmitRoomOnce = true;
Browser Support
DataChannel.js works fine on following browsers:
Browser | Support |
---|---|
Firefox | Stable / Aurora / Nightly |
Google Chrome | Stable / Canary / Beta / Dev |
Android | Chrome Beta |
License
DataChannel.js is released under MIT licence . Copyright (c) Muaz Khan.