DataChannel.js » A WebRTC Library for Data Sharing

HOME © Muaz Khan . @WebRTCWeb . Github . Latest issues . What's New?

Open New DataChannel Connection

Text Chat

Share Files

DataChannel.js Features:

  1. Direct messages — to any user using his `user-id`
  2. Eject/Reject any user — using his `user-id`
  3. Leave any room (i.e. data session) or close entire session using `leave` method
  4. File size is limitless!
  5. Text message length is limitless!
  6. Size of data is also limitless!
  7. Fallback to firebase/socket.io/websockets/etc.
  8. Users' presence detection using `onleave`
  9. Latency detection
  10. Multi-longest strings/files concurrent transmission

How to use DataChannel.js?

<script src="https://cdn.webrtc-experiment.com/DataChannel.js"> </script>

<input type="text" id="chat-input" disabled 
       style="font-size: 2em; width: 98%;"><br />
<div id="chat-output"></div>

<script>
    var chatOutput = document.getElementById('chat-output');
    var chatInput = document.getElementById('chat-input');
    chatInput.onkeypress = function(e) {
        if (e.keyCode != 13) return;
        channel.send(this.value);
        chatOutput.innerHTML = 'Me: ' + this.value + '<hr />' 
                             + chatOutput.innerHTML;
        this.value = '';
    };
</script>

<script>
    var channel = new DataChannel('Session Unique Identifier');

    channel.onopen = function(userid) {
        chatInput.disabled = false;
        chatInput.value = 'Hi, ' + userid;
        chatInput.focus();
    };

    channel.onmessage = function(message, userid) {
        chatOutput.innerHTML = userid + ': ' + message + '<hr />' 
                             + chatOutput.innerHTML;
    };

    channel.onleave = function(userid) {
        chatOutput.innerHTML = userid + ' Left.<hr />' 
                             + chatOutput.innerHTML;
    };
</script>

Use your own socket.io for signaling

<script>
    // by default Firebase is used for signaling; you can override it
    channel.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>

Latest Updates

Feedback

Enter your email too; if you want "direct" reply!