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:
- 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 socket.io/websockets/etc.
- Users' presence detection using `onleave`
- Latency detection
- 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 socket.io 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>