hyperpeer-node is a Node.js module for implementing the signaling server in applications based on Hyperpeer.
This module provides a single class called HyperpeerServer which manages the connection,
pairing and WebRTC signals exchange of Hyperpeer peers.
const HpServer = require('hyperpeer-node')
// Function used to validate and authorize peers
const verifyPeer = function(type, peerId, peerKey) {
const validTypes = new Set(['client', 'advisor', 'media-server'])
const peerIds = new Map([['client01', 'key001'], ['advisor01', 'key002'])
if (!validTypes.has(type)) return false
if (!peerIds.has(peerId)) return false
if (peerKey != peerIds.key(peerId)) return false
return true
}
// Instantiate the Hyperpeer server by automatically creating an HTTP server
const hpServer = new HpServer({ port: 3000, verifyPeer: verifyPeer })
// And that's it
console.log((new Date()) + ' Hyperpeer Server is listening on port 3000')
// Hyperpeer instances are also WebSocket.Server instances so you can listen to its events like the 'connection' event
hpServer.on('connection', () => {
const peers = hpServer.getPeers()
console.log('New peer connection. Connected peers: ' + peers.map((peer) => peer.id))
}) Kind: global class
Extends: WebSocket.Server
- HyperpeerServer ⇐
WebSocket.Server- new HyperpeerServer(options)
- instance
- inner
- ~verifyPeer :
function - ~peer :
Object
- ~verifyPeer :
Creates an instance of HyperpeerServer which is a wrapper of the WebSocket.Server class. HyperpeerServer instances manages the connection of peers, the pairing between peers, and relay messages between paired peers.
| Param | Type | Description |
|---|---|---|
| options | object |
Websocket server options (see ws API) |
| options.verifyPeer | verifyPeer |
A function that can be used to validate peers. If set, it replaces verifyClient attribute of WebSocket.Server |
hyperpeerServer.getPeers() ⇒ Array.<peer>
Returns the list of connected peers
Kind: instance method of HyperpeerServer
Funcition to verify the connection of a peer
Kind: inner typedef of HyperpeerServer
| Param | Type | Description |
|---|---|---|
| peerType | string |
Can be used to verify the category of the peer |
| peerId | string |
Unique identifier of the peer |
| peerKey | string |
Token that can be used to authenticate and authorize the peer |
Element of the list of peers.
Kind: inner typedef of HyperpeerServer
Properties
| Name | Type | Description |
|---|---|---|
| id | string |
id of the peer. |
| type | string |
type of the peer. |
| busy | boolean |
Indicates whether the peer is paired with another peer. |