npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

p2p-data-channel

v1.10.7

Published

Peer-to-peer (P2P) data channel over WebRTC and a signaling channel on the browser

Downloads

17

Readme

CircleCI

p2p-data-channel

p2p-data-channel is a TypeScript library that simplifies the process of creating a peer-to-peer (P2P) data channel using WebRTC for the main data channel and PeerJS as a signaling channel. With this library, developers can easily add peer-to-peer communication to their web applications without having to worry about the underlying details of WebRTC or PeerJS.

Installation

npm install p2p-data-channel

Usage

import P2PDataChannel from 'p2p-data-channel'

// default config values
const config = {
  debug: false, // output every method call to console
  dataChannel: 'default', // name of the data channel to be open
  connectionTimeout: 5000, // timeout to consider connection failure on init
  pingInterval: 4000, // interval for each ping/pong
  pingTimeout: 8000, // timeout to consider disconnection on ping/pong
}

const dataChannel = new P2PDataChannel('your-peer-id', config) // config is optional

// Receive a message
dataChannel.onMessage((message) => {
  console.log(`Received message from ${message.sender}: ${message.payload}`)
})

// Connect to a peer
dataChannel.connect('peer-id-to-connect').then(() => {
  // Send a message
  dataChannel.send('peer-id-to-connect', 'Hello, world!')
})

// Disconnect from the peer
dataChannel.disconnect('peer-id-to-disconnect')

API

connect(remotePeerId: PeerId): Promise<void>

Connects to the peer identified by remotePeerId.

Example:

dataChannel.connect('remote-peer-id')
  .then(() => {
    console.log('Connected to remote peer')
  })
  .catch((err) => {
    console.error(`Error connecting to remote peer: ${err}`)
  })

disconnect(remotePeerId: PeerId): void

Disconnects from the peer identified by remotePeerId.

Example:

dataChannel.disconnect('remote-peer-id')

onMessage(callback: P2PChannelMessageCallback<IMessagePayload>): void

Registers a callback to be called when a message is received from any peer.

Example:

dataChannel.onMessage((message) => {
  console.log(`Received message from ${message.sender}: ${message.payload}`);
})

onConnected(callback: (remotePeerId: PeerId) => void): void

Registers a callback to be called when a peer is connected.

Example:

dataChannel.onConnected((remotePeerId) => {
  console.log(`${remotePeerId} is connected!`);
})

onDisconnected(callback: (remotePeerId: PeerId) => void): void

Registers a callback to be called when a peer is disconnected.

Example:

dataChannel.onDisconnected((remotePeerId) => {
  console.log(`${remotePeerId} disconnected!`);
})

send(remotePeerId: PeerId, payload: IMessagePayload): void

Sends a message to the peer identified by remotePeerId.

throws ConnectionNotEstablished if not connected

Example:

dataChannel.send('remote-peer-id', 'Hello, world!') 

broadcast(message: IMessagePayload): void

Broadcasts a message to all connected peers.

Example:

dataChannel.broadcast('A new user has joined the chat!')

How it works

P2PDataChannel diagram

Contributing

If you find a bug or have a feature request, please open an issue on the GitHub repository. Pull requests are also welcome!

License

This project is licensed under the MIT License - see the LICENSE file for details.

Special Thanks

Special thanks to ChatGPT for being an excellent copilot throughout the development of this library.