wrtc-simple-data v0.0.4
wrtc-simple-data
A simple Node.js library to communicate between multiple peers via WebRTC data channels.
What is this?
This is a library which helps you with WebRTC communication between Node apps. You might have multiple apps that want to exchange data (e.g. logging, sensor data, ...) using a peer-to-peer connection. To do so you can use this lib to get set up p2p data channels between them with very low effort. Special is the multi-peer support. Usually a data channel exists between exactly two peers. If you want to have communication over more than just two peers but still without a central server (which probably is the reason why you even chose WebRTC instead of e.g. Websockets) you will need to build up a mesh. This basically means that each node hold a connection to (and a data channel with) every other node.
Signalling
Although the data communication is peer-to-peer with WebRTC you still need to have a central signalling server, which basically coordinates the connection establishment between the clients. You can (and really should) use https://github.com/n1try/wrtc-simple-data-signalling in combination with this project. All you need is this server to be running on an address and port of your choice.
Installation
npm install wrtc-simple-data
var dc = require('wrtc-simple-data')(opts)
or
git clone https://github.com/n1try/wrtc-simple-data
npm install
var dc = require('wrtc-simple-data')(opts)
And don't forget to install and start the signalling server...
Troubleshooting
This project depends on https://www.npmjs.com/package/wrtc. Please make sure to fulfil all of its prequesites (https://github.com/js-platform/node-webrtc#prerequisites) first, otherwise there will be compilation errors.
Usage example
var dc = require('wrtc-simple-data')({
roomName: 'chatRoom'
});
dc.on('ready', function () {
dc.on('channel:ready', function () {
console.log('Data channel is ready.');
});
dc.on('message', function (data) {
console.log(data.sender + ': ' + data.text);
});
dc.sendMessage('First message on this channel');
});
Also check out https://github.com/n1try/wrtc-simple-data/tree/master/examples.
API reference
Functions
.on('event', callback)
.sendMessage(messageText)
Events
ready => callback()
- Fired when connection establishment with the signalling server was successful and client is waiting for peerschannel:ready => callback()
- Fired when a data channel with a newly connected peer has successfully been openedchannel:notready => callback()
- Fired when a data channel isn't in open state anymoremessage => callback(data)
- Fired when receiving a messagedata: {sender: 'senderPeerId:string', text: 'messageText:string'}
Options
signallingServer
(default: 'http://localhost:3000/'): URL of the wrtc-simple-data-signalling serverroomName
(default: 'defaultRoom'): Socket.io room name where new peers connect to on the signalling serverrtcOpts
(default:{iceServers: freeice()}
): Options to pass to theRTCPeerConnection
objectchannelName
(default: 'messages'): Name of every created data channel - currently doesn't mean anythingchannelOpts
(default:{reliable: false}
): Options to pass to theRTCPeerConnection.createDataChannel()
methoddebugMode
(default: false): Turn on/off debug console logs