michal-wrzosek/p2p-chat
소개
Serverless peer to peer chat on WebRTC
웹 및 앱 개발측정된 스타 증가
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- 0
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
Originalp2p-chat
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
관련 프로젝트 살펴보기
불러오는 중…