• Stars
    star
    210
  • Rank 187,585 (Top 4 %)
  • Language
    HTML
  • License
    MIT License
  • Created about 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Serverless peer to peer chat on WebRTC

p2p-chat

npm

p2p chat on WebRTC with additional AES256 encryption and file sharing (no signaling server required)

>pitu-pitu chat<

The source code of demo chat is here

This is an example of how you can build p2p chat on WebRTC with no signaling servers. It should work in both Chrome and Firefox. WebRTC needs STUN and TURN servers to successfully establish p2p connection over the network. In my demo app I used some publicly available endpoints:

  • stun:stun.l.google.com:19302
  • turn:turn.anyfirewall.com:443?transport=tcp (webrtc:webrtc)

Additional features:

  • AES256 encryption to all messages and files
  • file sharing
  • chat available as a single HTML file with no dependencies over the network so you can just save that file locally index.html

Since there's no signaling server in between, you have to send a WebRTC connection description manually to your friend :D Sounds funny but it works (like 70% of times - sometimes you have to try to connect one more time by reloading a chat)

Library

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 iceServers: RTCIceServer[] = [
  {
    urls: 'stun:stun.l.google.com:19302',
  },
  {
    urls: 'turn:turn.anyfirewall.com:443?transport=tcp',
    username: 'webrtc',
    credential: 'webrtc',
  },
];

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({ iceServers, 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 iceServers: RTCIceServer[] = [
  {
    urls: 'stun:stun.l.google.com:19302',
  },
  {
    urls: 'turn:turn.anyfirewall.com:443?transport=tcp',
    username: 'webrtc',
    credential: 'webrtc',
  },
];

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, iceServers, 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/...


This package was bootstrapped with typescript-lib-boilerplate

More Repositories

1

react-component-lib

Boilerplate repo for creating npm packages with React components written in TypeScript and using styled-components
TypeScript
73
star
2

cntl

Small library that helps you write your class names in a more clear, readable and composable way. Especially useful with tailwind css.
JavaScript
62
star
3

web-admin-api

Web, Admin & API - TypeScript, React, Next.js GraphQL, Apollo, Express, Docker, Mongo monorepo boilerplate
TypeScript
10
star
4

ufos-refresher

r/UFOs refresher - Simple way to stay up to date with latest UFO news
TypeScript
4
star
5

apollo-boost-upload

Basically ApolloBoost but you can upload files with it
TypeScript
4
star
6

next-styled-components

Styled-Components that are compatible with Next.js server components (RSC)
TypeScript
4
star
7

moorage

Moorage - Server side rendering masonry like react layout component
TypeScript
3
star
8

zksync-messages

Ethereum zkSync messages dapp
TypeScript
2
star
9

michal-wrzosek

My personal website
TypeScript
2
star
10

typescript-lib-boilerplate

Typescript library boilerplate => CRA example app, eslint, prettier, vscode, rollup
JavaScript
2
star
11

next-data-streaming

Next.js progressive partial data streaming library
TypeScript
2
star
12

create-react-app-boilerplate

Create React App Boilerplate - Typescript import alias + eslint with prettier
TypeScript
2
star
13

parcel-typescript

Parcel + Typescript starter - Small starter repo for simple typescript web apps
JavaScript
1
star
14

schemat

Schemat - simple runtime schema validator
JavaScript
1
star
15

gtd-inbox

React Native "Getting Things Done" Inbox iOS & Android App
TypeScript
1
star
16

subscribe-context

React Hooks for Subscribable Subject in Context Pattern
TypeScript
1
star
17

challenge-app

Challenge App - Demo SPA connected to REST API with JWT Auth
TypeScript
1
star
18

xfor-text-field

XFor Text Field React Component
TypeScript
1
star
19

worp

worp - fixtures factory / generator
JavaScript
1
star
20

challenge-api

Challenge API - REST API
TypeScript
1
star
21

future-predictions

Forex bot - statistical analysis of GBP/USD historical data (TypeScript + React App)
TypeScript
1
star
22

kubernetes-config

My Kubernetes config
1
star
23

express-boilerplate

Express + Typescript boilerplate
TypeScript
1
star
24

xfor-typehead

React typehead input component with autocomplete
JavaScript
1
star
25

vaccination-certificates

Vaccination Certificates Blockchain Ethereum DAPP
TypeScript
1
star
26

wordpress-setup

Example configuration for setting up wordpress with docker-compose
Shell
1
star