peer-data

peer-data

  • Docs
  • Help
  • GitHub

›Connection

Quick Start

  • Installation
  • Basic Example
  • Signaling Server

Connection

  • Data Channel
  • Media Stream

Examples

  • Error Handling
  • Local connection
  • Socket signalling channel
  • Data channel example

Data Channel

What is a data channel?

A WebRTC data channel lets you send text or binary data over an active connection to a peer. In the context of a game, this lets players send data to each other, whether text chat or game status information. Data channels come in two flavors.

Example

import PeerData, { EventDispatcher, SocketChannel } from 'peer-data';

const constraints = {ordered: true};
const servers = {
  iceServers: [
    {urls: "stun:stun.1.google.com:19302"}
  ]
};

const dispatcher = new EventDispatcher();
const peerData = new PeerData(dispatcher, servers, constraints);
const signaling = new SocketChannel(dispatcher, 'http://localhost:8080');

const room = peerData.connect('test-room');

//payload object can be everything
const payload = {msg: "Hi there!", date: now()};
//send to everybody in room
room.send(payload);

room.on("participant", participant => {
    //this peer sent a message
    participant.on("message", payload => console.log("message", payload));

    //send only to participant
    participant.send('Hi mate! this is private message.');
});
← Signaling ServerMedia Stream →
  • What is a data channel?
  • Example
peer-data
Docs
Documentation
Community
Support
More
rafallorenz.comGitHubStar
Copyright © 2022 Rafał Lorenz