Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# @thaunknown/simple-peer [![coveralls][coveralls-image]][coveralls-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]

[coveralls-image]: https://coveralls.io/repos/github/thaunknown/simple-peer/badge.svg?branch=master
[coveralls-url]: https://coveralls.io/github/thaunknown/simple-peer?branch=master
[npm-image]: https://img.shields.io/npm/v/@thaunknown/simple-peer.svg
[npm-url]: https://npmjs.org/package/@thaunknown/simple-peer
[downloads-image]: https://img.shields.io/npm/dm/@thaunknown/simple-peer.svg
[downloads-url]: https://npmjs.org/package/@thaunknown/simple-peer
# @workadventure/simple-peer [![coveralls][coveralls-image]][coveralls-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]

[coveralls-image]: https://coveralls.io/repos/github/workadventure/simple-peer/badge.svg?branch=master
[coveralls-url]: https://coveralls.io/github/workadventure/simple-peer?branch=master
[npm-image]: https://img.shields.io/npm/v/@workadventure/simple-peer.svg
[npm-url]: https://npmjs.org/package/@workadventure/simple-peer
[downloads-image]: https://img.shields.io/npm/dm/@workadventure/simple-peer.svg
[downloads-url]: https://npmjs.org/package/@workadventure/simple-peer
[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
[standard-url]: https://standardjs.com

#### Simple WebRTC video, voice, and data channels

This package is a more up-to-date version of feross's simple-peer, rewritten 100% in Typescript. It uses `EventEmitter` and `Uint8Array` instead of node streams and buffers, making the module lighter and fully compatible with browser bundlers like Vite. It provides `@thaunknown/simple-peer/lite.js` which is the same peer implementation, but without MediaTrack and MediaStream handling, just pure Uint8/String data.

This package is a more up-to-date version of feross's simple-peer, rewritten 100% in Typescript. It uses `EventEmitter` and `Uint8Array` instead of node streams and buffers, making the module lighter and fully compatible with browser bundlers like Vite. It provides `@workadventure/simple-peer/lite.ts` which is the same peer implementation, but without MediaTrack and MediaStream handling, just pure Uint8/String data.

> **Breaking change:** This version no longer extends `Duplex` stream. The `pipe()` method is not available. Use `peer.on('data', ...)` and `peer.send()` / `peer.write()` instead.

Expand Down Expand Up @@ -53,7 +52,20 @@ This package is used by [WebTorrent](https://webtorrent.io) and [many others](#w
npm install @workadventure/simple-peer
```

This package works in the browser with a bundler like Vite.
This package works with modern bundlers (Vite, Rollup, Webpack) and also directly in the browser via native ESM.
If you do not use a bundler, include it using a `<script type="module">` and import from a CDN:

```html
<script type="module">
import Peer from 'https://cdn.jsdelivr.net/npm/@workadventure/simple-peer@12/dist/index.js'
// use Peer here
</script>
```

Variants are available as separate entry points:

- Lite: `@workadventure/simple-peer/lite`
- Full: `@workadventure/simple-peer/full`

## usage

Expand All @@ -74,9 +86,10 @@ Let's create an html page that lets you manually connect two peers:
<button type="submit">submit</button>
</form>
<pre id="outgoing"></pre>
<script src="simplepeer.min.js"></script>
<script>
const p = new SimplePeer({
<script type="module">
import Peer from 'https://cdn.jsdelivr.net/npm/@workadventure/simple-peer@12/dist/index.js'

const p = new Peer({
initiator: location.hash === '#1',
trickle: false
})
Expand Down Expand Up @@ -127,7 +140,7 @@ peer-to-peer connection is established.
### data channels

```js
var Peer = require('simple-peer')
var Peer = require('@workadventure/simple-peer')

var peer1 = new Peer({ initiator: true })
var peer2 = new Peer()
Expand Down Expand Up @@ -158,7 +171,7 @@ peer2.on('data', data => {
Video/voice is also super simple! In this example, peer1 sends video to peer2.

```js
var Peer = require('simple-peer')
var Peer = require('@workadventure/simple-peer')

// get video/voice stream
navigator.mediaDevices.getUserMedia({
Expand Down Expand Up @@ -203,7 +216,7 @@ It is also possible to establish a data-only connection at first, and later add
a video/voice stream, if desired.

```js
var Peer = require('simple-peer') // create peer without waiting for media
var Peer = require('@workadventure/simple-peer') // create peer without waiting for media

var peer1 = new Peer({ initiator: true }) // you don't need streams here
var peer2 = new Peer()
Expand Down Expand Up @@ -245,7 +258,7 @@ navigator.mediaDevices.getUserMedia({
To use this library in node, pass in `opts.wrtc` as a parameter (see [the constructor options](#peer--new-peeropts)):

```js
var Peer = require('simple-peer')
var Peer = require('@workadventure/simple-peer')
var wrtc = require('wrtc')

var peer1 = new Peer({ initiator: true, wrtc: wrtc })
Expand Down
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"typescript": "^5.9.3",
"util": "^0.12.5",
"vite": "^7.2.7",
"vite-plugin-dts": "^4.5.4",
"vitest": "^4.0.15",
"ws": "^8.18.0"
},
Expand All @@ -59,19 +60,26 @@
"webrtc stream"
],
"license": "MIT",
"main": "dist/index.js",
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"default": "./dist/index.js"
},
"./lite": {
"types": "./dist/lite.d.ts",
"import": "./dist/lite.js",
"require": "./dist/lite.cjs",
"default": "./dist/lite.js"
},
"./full": {
"types": "./dist/full.d.ts",
"import": "./dist/full.js",
"require": "./dist/full.cjs",
"default": "./dist/full.js"
}
},
Expand All @@ -83,7 +91,7 @@
"url": "git://github.com/workadventure/simple-peer.git"
},
"scripts": {
"build": "tsc",
"build": "vite build",
"prebuild": "rm -rf dist",
"prepublishOnly": "npm run build",
"test": "npm run build && vitest run",
Expand Down
Loading