New:Microsoft Teams Notifications Are Now Available in Socket.Learn more →
Get Started

p2p-chat

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

p2p-chat

![npm](https://img.shields.io/npm/v/p2p-chat)

Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
4
Maintainers
1
Weekly downloads
 
Created
Source

p2p-chat

npm

Serverless peer to peer chat built on WebRTC.

Demo chat

This is an example of how you can build p2p chat on WebRTC. Currently it works only in Firefox. As of writing this package (November 2019) Chrome STUN servers connection doesn't work and if 2 parties are in different networks, p2p connection will not work.

I made a small wrapper around WebRTC for the purpose of constructing a demo chat. It boils down to a function I called createPeerConnection.

To install:

npm install --save p2p-chat

To use:

To initiate a new connection (as a HOST):

import { createPeerConnection } from 'p2p-chat';

const someAsyncFunc = async () => {
  const onChannelOpen = () => console.log(`Connection ready!`);
  const onMessageReceived = (message: string) => console.log(`New incomming message: ${message}`);
  
  const { localDescription, setAnswerDescription, sendMessage } = await createPeerConnection({ onMessageReceived, onChannelOpen });
  
  // you will send localDescription to your SLAVE and he will give you his localDescription. You will set it as an answer to establish connection
  const answerDescription = 'This is a string you will get from a SLAVE trying to connect with your localDescription';
  setAnswerDescription(answerDescription);
  
  // later on you can send a message to SLAVE
  sendMessage('Hello SLAVE');
}

To join a connection (as a SLAVE):

import { createPeerConnection } from 'p2p-chat';

const someAsyncFunc = async () => {
  const remoteDescription = 'This is a string you will get from a host...';
  const onChannelOpen = () => console.log(`Connection ready!`);
  const onMessageReceived = (message: string) => console.log(`New incomming message: ${message}`);
  
  const { localDescription, sendMessage } = await createPeerConnection({ remoteDescription, onMessageReceived, onChannelOpen });
  
  // Send your local description to HOST and wait for a connection to start
  
  // Later on you can send a message to HOST
  sendMessage('Hello HOST');
};

You can take a look how I implemented it in a demo chat app: example/src/App.tsx

Typescript

This lib has types already built in. No need for @types/...

FAQs

Package last updated on 03 Nov 2019

Related posts