Comparing version 0.3.2 to 0.4.0
# PeerServer Changelog | ||
### 0.4.0 | ||
* New: Allow passing in custom client ID generation function - #157 Thanks @ajmar | ||
### 0.3.2 | ||
* Fixed: fix main field in package.json | ||
### 0.3.1 | ||
@@ -4,0 +12,0 @@ |
@@ -12,3 +12,3 @@ "use strict"; | ||
res.contentType("html"); | ||
res.send(realm.generateClientId()); | ||
res.send(realm.generateClientId(config.generateClientId)); | ||
}); | ||
@@ -15,0 +15,0 @@ // Get a list of all peers for a key, enabled by the `allowDiscovery` flag. |
@@ -44,6 +44,7 @@ "use strict"; | ||
} | ||
generateClientId() { | ||
let clientId = v4_1.default(); | ||
generateClientId(generateClientId) { | ||
const generateId = generateClientId ? generateClientId : v4_1.default; | ||
let clientId = generateId(); | ||
while (this.getClientById(clientId)) { | ||
clientId = v4_1.default(); | ||
clientId = generateId(); | ||
} | ||
@@ -50,0 +51,0 @@ return clientId; |
{ | ||
"name": "peer", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"description": "PeerJS server component", | ||
@@ -9,2 +9,6 @@ "main": "dist/src/index.js", | ||
}, | ||
"files": [ | ||
"bin/", | ||
"dist/" | ||
], | ||
"repository": { | ||
@@ -17,2 +21,3 @@ "type": "git", | ||
"scripts": { | ||
"preversion": "npm run clean && npm run build", | ||
"build": "tsc", | ||
@@ -19,0 +24,0 @@ "clean": "rimraf ./dist", |
177
README.md
[![Build Status](https://travis-ci.org/peers/peerjs-server.png?branch=master)](https://travis-ci.org/peers/peerjs-server) | ||
[![npm version](https://badge.fury.io/js/peer.svg)](https://www.npmjs.com/package/peer) | ||
[![Downloads](https://img.shields.io/npm/dm/peer.svg)](https://www.npmjs.com/package/peer) | ||
[![Docker Image Size (latest semver)](https://img.shields.io/docker/image-size/peerjs/peerjs-server)](https://hub.docker.com/r/peerjs/peerjs-server) | ||
# PeerServer: A server for PeerJS # | ||
@@ -13,45 +13,75 @@ | ||
## [https://peerjs.com](https://peerjs.com) | ||
### [https://peerjs.com](https://peerjs.com) | ||
### Run PeerServer | ||
## Usage | ||
1. Install PeerServer from npm or github: | ||
### Run server | ||
If you don't want to develop anything, just enter a few commands below. | ||
#### NPM | ||
```bash | ||
npm install peer | ||
``` | ||
1. Install the package globally: | ||
```sh | ||
$ npm install peer -g | ||
``` | ||
2. Run the server: | ||
```sh | ||
$ peerjs --port 9000 --key peerjs --path /myapp | ||
#### github | ||
Started PeerServer on ::, port: 9000, path: /myapp (v. 0.3.2) | ||
``` | ||
3. Check it: http://127.0.0.1:9000/myapp It should returns JSON with name, description and website fields. | ||
```bash | ||
git clone https://github.com/peers/peerjs-server.git#master | ||
npm install | ||
Also, you can use Docker image to run a new container: | ||
```sh | ||
$ docker run -p 9000:9000 -d peerjs/peerjs-server | ||
``` | ||
2. Run the server: | ||
### Create a custom server: | ||
If you have your own server, you can attach PeerServer. | ||
```bash | ||
$> peerjs --port 9000 --key peerjs --path /myapp | ||
``` | ||
1. Install the package: | ||
```sh | ||
#$ cd your-project-path | ||
$ npm install peer | ||
``` | ||
2. Use PeerServer object to create a new server: | ||
```javascript | ||
const { PeerServer } = require('peer'); | ||
Or, create a custom server: | ||
const peerServer = PeerServer({ port: 9000, path: '/myapp' }); | ||
``` | ||
```javascript | ||
const { PeerServer } = require('peer'); | ||
const server = PeerServer({port: 9000, path: '/myapp'}); | ||
``` | ||
3. Check it: http://127.0.0.1:9000/myapp It should returns JSON with name, description and website fields. | ||
3. Check that server works: open browser with [http://localhost:9000/myapp](http://localhost:9000/myapp) It should returns JSON with name, description and website fields. | ||
### Connecting to the server from client PeerJS: | ||
### Connecting to the server from PeerJS: | ||
```html | ||
<script> | ||
const peer = new Peer('someid', {host: 'localhost', port: 9000, path: '/myapp'}); | ||
const peer = new Peer('someid', { | ||
host: 'localhost', | ||
port: 9000, | ||
path: '/myapp' | ||
}); | ||
</script> | ||
``` | ||
### Using HTTPS: Simply pass in PEM-encoded certificate and key. | ||
## Config / CLI options | ||
You can provide config object to `PeerServer` function or specify options for `peerjs` CLI. | ||
| CLI option | JS option | Description | Required | Default | | ||
| -------- | ------- | ------------- | :------: | :---------: | | ||
| `--port, -p` | `port` | Port to listen (number) | **Yes** | | | ||
| `--key, -k` | `key` | Connection key (string). Client must provide it to call an API methods | No | `"peerjs"` | | ||
| `--path` | `path` | Path (string). The server responds for requests to the root URL + path. **E.g.** Set the `path` to `/myapp` and run server on 9000 port via `peerjs --port 9000 --path /myapp` Then open http://127.0.0.1:9000/myapp - you should see a JSON reponse. | No | `"/"` | | ||
| `--proxied` | `proxied` | Set `true` if PeerServer stays behind a reverse proxy (boolean) | No | `false` | | ||
| `--expire_timeout, -t` | `expire_timeout` | The amount of time after which an `EXPIRE` message will be sent to initiator (milliseconds). This mean that other client didn't have a connection with server. | No | `5000` | | ||
| `--alive_timeout` | `alive_timeout` | Timeout for broken connection (milliseconds). If the server doesn't receive any data from client (includes `pong` messages), the client's connection will be destroyed. | No | `60000` | | ||
| `--concurrent_limit, -c` | `concurrent_limit` | Maximum number of clients' connections to WebSocket server (number) | No | `5000` | | ||
| `--sslkey` | `sslkey` | Path to SSL key (string) | No | | | ||
| `--sslcert` | `sslcert` | Path to SSL certificate (string) | No | | | ||
| `--allow_discovery` | `allow_discovery` | Allow to use `/peers` API method to get an array of ids of all connected clients (boolean) | No | | | ||
| | `generateClientId` | A function which generate random client IDs when executes `/id` API method (`() => string`) | No | `uuid/v4` | | ||
## Using HTTPS | ||
Simply pass in PEM-encoded certificate and key. | ||
```javascript | ||
@@ -61,3 +91,3 @@ const fs = require('fs'); | ||
const server = PeerServer({ | ||
const peerServer = PeerServer({ | ||
port: 9000, | ||
@@ -71,3 +101,3 @@ ssl: { | ||
### Running PeerServer behind a reverse proxy | ||
## Running PeerServer behind a reverse proxy | ||
@@ -81,14 +111,39 @@ Make sure to set the `proxied` option, otherwise IP based limiting will fail. | ||
const { PeerServer } = require('peer'); | ||
const server = PeerServer({port: 9000, path: '/myapp', proxied: true}); | ||
const peerServer = PeerServer({ | ||
port: 9000, | ||
path: '/myapp', | ||
proxied: true | ||
}); | ||
``` | ||
### Combining with existing express app | ||
## Custom client ID generation | ||
By default, PeerServer uses `uuid/v4` npm package to generate random client IDs. | ||
You can set `generateClientId` option in config to specify a custom function to generate client IDs. | ||
```javascript | ||
const { PeerServer } = require('peer'); | ||
const customGenerationFunction = () => (Math.random().toString(36) + '0000000000000000000').substr(2, 16); | ||
const peerServer = PeerServer({ | ||
port: 9000, | ||
path: '/myapp', | ||
generateClientId: customGenerationFunction | ||
}); | ||
``` | ||
Open http://127.0.0.1:9000/myapp/peerjs/id to see a new random id. | ||
## Combining with existing express app | ||
```javascript | ||
const express = require('express'); | ||
const app = express(); | ||
const { ExpressPeerServer } = require('peer'); | ||
app.get('/', (req, res, next) => { res.send('Hello world!'); }); | ||
const app = express(); | ||
app.get('/', (req, res, next) => res.send('Hello world!')); | ||
// ======= | ||
@@ -98,18 +153,21 @@ | ||
const options = { | ||
debug: true | ||
path: '/peerjs' | ||
} | ||
const peerServer = ExpressPeerServer(server, { | ||
debug: true, | ||
path: '/myapp' | ||
}); | ||
const peerserver = ExpressPeerServer(server, options); | ||
app.use('/peerjs', peerServer); | ||
app.use(options.path, peerserver); | ||
// == OR == | ||
const server = require('http').createServer(app); | ||
const peerserver = ExpressPeerServer(server, options); | ||
const http = require('http'); | ||
app.use(options.path, peerserver); | ||
const server = http.createServer(app); | ||
const peerServer = ExpressPeerServer(server, { | ||
debug: true, | ||
path: '/myapp' | ||
}); | ||
app.use('/peerjs', peerServer); | ||
server.listen(9000); | ||
@@ -120,8 +178,10 @@ | ||
### Events | ||
Open the browser and check http://127.0.0.1:9000/peerjs/myapp | ||
## Events | ||
The `'connection'` event is emitted when a peer connects to the server. | ||
```javascript | ||
peerserver.on('connection', (client) => { ... }); | ||
peerServer.on('connection', (client) => { ... }); | ||
``` | ||
@@ -133,3 +193,3 @@ | ||
```javascript | ||
peerserver.on('disconnect', (client) => { ... }); | ||
peerServer.on('disconnect', (client) => { ... }); | ||
``` | ||
@@ -139,4 +199,4 @@ | ||
```bash | ||
npm test | ||
```sh | ||
$ npm test | ||
``` | ||
@@ -146,14 +206,25 @@ | ||
You can build this image simply by calling: | ||
```bash | ||
docker build -t peerjs https://github.com/peers/peerjs-server.git | ||
We have 'ready to use' images on docker hub: | ||
https://hub.docker.com/r/peerjs/peerjs-server | ||
To run the latest image: | ||
```sh | ||
$ docker run -p 9000:9000 -d peerjs/peerjs-server | ||
``` | ||
You can build a new image simply by calling: | ||
```sh | ||
$ docker build -t myimage https://github.com/peers/peerjs-server.git | ||
``` | ||
To run the image execute this: | ||
```bash | ||
docker run -p 9000:9000 -d peerjs | ||
```sh | ||
$ docker run -p 9000:9000 -d myimage | ||
``` | ||
This will start a peerjs server on port 9000 exposed on port 9000. | ||
This will start a peerjs server on port 9000 exposed on port 9000 with key `peerjs` on path `/myapp`. | ||
Open browser with http://localhost:9000/myapp It should returns JSON with name, description and website fields. http://localhost:9000/myapp/peerjs/id - should returns a random string (random client id) | ||
## Problems? | ||
@@ -160,0 +231,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
227
37407
29
692