Socket using the AppRtc WebSocket server to exchange messages between two peers. This socket is developed to exchange lightweight signaling messages, not for transmitting (large) binaries.
- supports text message exchange between two peers identified by a unique key
- offers callback and promise based API
- can be browserified
npm install apprtc-socket
const socket = require('apprtc-socket');
const myId = 'foo'; // replace with unique key
const peerId = 'bar'; // replace with unique key
const mySocket = socket(myId, peerId);
// socket is connected and ready to use
mySocket.on('ready', () => {
// send text message to peer
mySocket.send('test message');
// close socket
mySocket.close();
});
mySocket.on('message', (message) => {
// incoming text message
});
mySocket.on('close', () => {
// socket is closed
});
mySocket.on('error', (error) => {
// ooops
});
// activate the connection
mySocket.connect();const socket = require('apprtc-socket');
const myId = 'foo'; // replace with unique key
const peerId = 'bar'; // replace with unique key
const mySocket = socket(myId, peerId);
mySocket.on('message', (message) => {
// incoming text message
});
mySocket.on('error', (error) => {
// ooops
});
// activate the connection
mySocket.connectP()
// socket is connected and ready to use
.then(() => {
// send text message to peer
mySocket.send('test message');
// close socket
return mySocket.closeP();
})
.then(() => {
// socket is closed
})
.catch((error) => {
// ooops
});Create a new AppRtcSocket connection. Both interacting peers must be identified by a unique key.
Connect to the AppRtc WebSocket server and register a connection. The id of the AppRtc room that both peers use to exchange messages is a commutative hash calculated from both peers' unique keys. This function fires a ready event once the registration succeeds.
Connect to the AppRtc WebSocket server and register a connection. Instead of firing a ready event, this function returns a promise that gets fulfilled once the registration succeeds.
Send text data to the connected peer. data should be of type
String, other data types can be transformed into a String with JSON.stringify.
Close a connection. Fires a close event once complete.
Close a connection. Instead of firing a close event, this function returns a promise that gets fulfilled once the connection has closed.
Fired when the socket is ready to use.
Received a message from the AppRtc server. message is always a String.
Fired when the connection has closed.
Fired when a fatal error occurs.
See examples directory.