Skip to content

Commit 0e90c13

Browse files
committed
init
0 parents  commit 0e90c13

13 files changed

+7450
-0
lines changed

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
node_modules
2+
coverage
3+
.nyc_output
4+
.DS_Store
5+
*.log
6+
.vscode
7+
.idea
8+
dist
9+
compiled
10+
.awcache
11+
.rpt2_cache
12+
docs

LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2017 Matt Krick <[email protected]>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# fast-rtc-swarm
2+
3+
A full-mesh WebRTC swarm built on top of WebSockets & fast-rtc-peer
4+
5+
## Installation
6+
7+
`yarn add @mattkrick/fast-rtc-swarm`
8+
9+
## What is it
10+
11+
[fast-rtc-peer](https://github.com/mattkrick/fast-rtc-peer) offers a great API to connect 2 peers.
12+
If you'd like to connect more than 2 peers, you're on your own.
13+
That's why this exists.
14+
It uses a full mesh (vs. partial mesh) network so every client is connected to every other client.
15+
A full mesh is great for up to ~100 connections.
16+
After that, you'll probably want to move to a partial mesh & trade a little latency for memory
17+
18+
## How's it different from webrtc-swarm?
19+
20+
fast-rtc-swarm is different.
21+
- The signaling server doesn't have to be server sent events. It can be anything (reference implementation is websockets)
22+
- It doesn't bother the signaling server with a heartbeat. The swarm can give us that info.
23+
- It only connects to 1 signaling server (IMO multiple servers is a proxy problem, not a client problem)
24+
- No unauthenticated-by-default signaling server CLI, It's forcing you to implement your own in hopes you implement auth.
25+
- No multiplexing streams. If you need a new data channel, open another one natively, not with expensive streams.
26+
- It uses the fast-rtc protocol for the fastest connection possible
27+
28+
## What makes it so fast?
29+
30+
the fast-rtc normally completes a WebRTC handshake in only 2 round trips.
31+
Most other implementations take 3 (or even 4!)
32+
It does this by keeping a 1-length cache of offers and candidates.
33+
Think of it like "pay-it-forward".
34+
You pay for the coffee for the person behind you, so they buy the coffee of the person behind them.
35+
36+
Here's how it works:
37+
38+
Alice is the first peer to join the swarm:
39+
- She gives the server an OFFER that can be used by the next person to join
40+
- As CANDIDATES trickle in, she forwards those to the signaling server
41+
- If the OFFER has been accepted, the server forwards the CANDIDATE to them, else it stores it with her OFFER
42+
- When someone takes her OFFER, the server REQUESTS another from her
43+
44+
When Bob joins the swarm:
45+
- He follows the same protocol as Alice
46+
- He takes all the unclaimed OFFERS and CANDIDATES on the server
47+
- On the client, Bob creates an ANSWER to each OFFER and forwards it to the signaling server
48+
- The signaling server forwards Bob's ANSWER to Alice
49+
- Alice uses Bob's ANSWER to initiate the connection
50+
51+
That's it! See `server.js` for a reference implementation or an example below to see how to add it to your own server.
52+
53+
## Usage
54+
55+
```js
56+
// client
57+
import FastRTCSwarm from '@mattkrick/fast-rtc-swarm'
58+
59+
const socket = new WebSocket('ws://localhost:3000');
60+
socket.addEventListener('open', () => {
61+
const swarm = new FastRTCSwarm()
62+
// send the signal to the signaling server
63+
swarm.on('signal', (signal) => {
64+
socket.send(JSON.stringify(signal))
65+
})
66+
// when the signal come back, dispatch it to the swarm
67+
socket.addEventListener('message', (event) => {
68+
const payload = JSON.parse(event.data)
69+
swarm.dispatch(payload)
70+
})
71+
// when the connection is open, say hi to your new peer
72+
swarm.on('dataOpen', (peer) => {
73+
console.log('data channel open!')
74+
peer.send('hi')
75+
})
76+
// when your peer says hi, log it
77+
swarm.on('data', (data, peer) => {
78+
console.log('data received', data, peer)
79+
})
80+
})
81+
82+
83+
// server
84+
import handleOnMessage from '@mattkrick/fast-rtc-swarm/server'
85+
86+
wss.on('connection', (ws) => {
87+
ws.on('message', (message) => {
88+
const payload = JSON.parse(message)
89+
// Check your perms here!
90+
if (handleOnMessage(wss.clients, ws, payload)) return
91+
// your other websocket handlers here
92+
})
93+
})
94+
```
95+
96+
## API
97+
98+
Options: A superset of `RTCConfiguration`.
99+
To add a TURN server to the default list of ICE candidates, see [fast-rtc-peer](https://github.com/mattkrick/fast-rtc-peer).
100+
- `id`: An ID to assign to the peer, defaults to a v4 uuid
101+
- `wrtc`: pass in [node-webrtc](https://github.com/js-platform/node-webrtc) if using server side
102+
103+
Methods on FastRTCSwarm
104+
- `dispatch(signal)`: receive an incoming signal from the signal server
105+
- `broadcast(message)`: send a string or buffer to all connected peers
106+
- `close()`: destroy all peer connections
107+
108+
## Events
109+
110+
- `swarm.on('dataOpen', (peer) => {})`: fired when a peer connects
111+
- `swarm.on('dataClose', (peer) => {})`: fired when a peer disconnects
112+
- `swarm.on('data', (data, peer) => {})`: fired when a peer sends data
113+
- `swarm.on('signal', (signal, peer) => {})`: fired when a peer creates an offer, ICE candidate, or answer.
114+
115+
## License
116+
117+
MIT

code-of-conduct.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Code of Conduct
2+
3+
don't be a dick.

package.json

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
{
2+
"name": "@mattkrick/fast-rtc-swarm",
3+
"version": "0.0.1",
4+
"description": "A full-mesh WebRTC swarm built on top of simple-peer",
5+
"keywords": [
6+
"WebRTC"
7+
],
8+
"main": "dist/FastRTCSwarm.js",
9+
"typings": "dist/types/FastRTCSwarm.d.ts",
10+
"files": [
11+
"dist"
12+
],
13+
"author": "Matt Krick <[email protected]>",
14+
"repository": {
15+
"type": "git",
16+
"url": "https://github.com/mattkrick/fast-rtc-swarm"
17+
},
18+
"license": "MIT",
19+
"engines": {
20+
"node": ">=6.0.0"
21+
},
22+
"scripts": {
23+
"build": "webpack --config webpack.config.js"
24+
},
25+
"lint-staged": {
26+
"{src,test}/**/*.ts": [
27+
"prettier --write",
28+
"git add"
29+
]
30+
},
31+
"config": {
32+
"commitizen": {
33+
"path": "node_modules/cz-conventional-changelog"
34+
},
35+
"validate-commit-msg": {
36+
"types": "conventional-commit-types",
37+
"helpMessage": "Use \"npm run commit\" instead, we use conventional-changelog format :) (https://github.com/commitizen/cz-cli)"
38+
}
39+
},
40+
"jest": {
41+
"transform": {
42+
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
43+
},
44+
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
45+
"moduleFileExtensions": [
46+
"ts",
47+
"tsx",
48+
"js"
49+
],
50+
"coveragePathIgnorePatterns": [
51+
"/node_modules/",
52+
"/test/"
53+
],
54+
"coverageThreshold": {
55+
"global": {
56+
"branches": 90,
57+
"functions": 95,
58+
"lines": 95,
59+
"statements": 95
60+
}
61+
},
62+
"collectCoverage": true
63+
},
64+
"prettier": {
65+
"semi": false,
66+
"singleQuote": true
67+
},
68+
"devDependencies": {
69+
"@types/jest": "^22.0.0",
70+
"@types/node": "^10.0.3",
71+
"@types/ws": "^5.1.2",
72+
"awesome-typescript-loader": "^5.2.0",
73+
"colors": "^1.1.2",
74+
"commitizen": "^2.9.6",
75+
"coveralls": "^3.0.0",
76+
"cross-env": "^5.0.1",
77+
"css-loader": "^1.0.0",
78+
"cz-conventional-changelog": "^2.0.0",
79+
"husky": "^0.14.0",
80+
"jest": "^22.0.2",
81+
"lint-staged": "^7.1.3",
82+
"lodash.camelcase": "^4.3.0",
83+
"prettier": "^1.13.4",
84+
"prompt": "^1.0.0",
85+
"rimraf": "^2.6.1",
86+
"semantic-release": "^15.0.0",
87+
"simple-peer": "^9.1.2",
88+
"ts-jest": "^22.0.0",
89+
"ts-node": "^7.0.0",
90+
"tslint": "^5.8.0",
91+
"tslint-config-prettier": "^1.1.0",
92+
"tslint-config-standard": "^7.0.0",
93+
"typedoc": "^0.11.0",
94+
"typescript": "^2.6.2",
95+
"uuid": "^3.3.2",
96+
"validate-commit-msg": "^2.12.2",
97+
"webpack": "^4.16.1",
98+
"webpack-cli": "^3.1.0",
99+
"ws": "^6.0.0"
100+
},
101+
"dependencies": {
102+
"eventemitter3": "^3.1.0",
103+
"webrtc-adapter": "^6.3.0"
104+
}
105+
}

0 commit comments

Comments
 (0)