Skip to content

Commit

Permalink
Merge pull request #42 from openculinary/server/bind-host-variable
Browse files Browse the repository at this point in the history
Add HOST environment variable to configure websocket-server bind address
  • Loading branch information
dmonad authored Jan 6, 2021
2 parents 2a3d623 + 1c278ad commit 3ab927f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ wsOpts = {
Start a y-websocket server:

```sh
PORT=1234 npx y-websocket-server
HOST=localhost PORT=1234 npx y-websocket-server
```

Since npm symlinks the `y-websocket-server` executable from your local `./node_modules/.bin` folder, you can simply run npx. The `PORT` environment variable already defaults to 1234.
Since npm symlinks the `y-websocket-server` executable from your local `./node_modules/.bin` folder, you can simply run npx. The `PORT` environment variable already defaults to 1234, and `HOST` defaults to `localhost`.

### Websocket Server with Persistence

Expand All @@ -97,7 +97,7 @@ Persist document updates in a LevelDB database.
See [LevelDB Persistence](https://github.com/yjs/y-leveldb) for more info.

```sh
PORT=1234 YPERSISTENCE=./dbDir node ./node_modules/y-websocket/bin/server.js
HOST=localhost PORT=1234 YPERSISTENCE=./dbDir node ./node_modules/y-websocket/bin/server.js
```

### Websocket Server with HTTP callback
Expand Down
5 changes: 3 additions & 2 deletions bin/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const http = require('http')
const wss = new WebSocket.Server({ noServer: true })
const setupWSConnection = require('./utils.js').setupWSConnection

const host = process.env.HOST || 'localhost'
const port = process.env.PORT || 1234

const server = http.createServer((request, response) => {
Expand All @@ -28,6 +29,6 @@ server.on('upgrade', (request, socket, head) => {
wss.handleUpgrade(request, socket, head, handleAuth)
})

server.listen(port)
server.listen({host, port})

console.log('running on port', port)
console.log(`running at '${host}' on port ${port}`)

0 comments on commit 3ab927f

Please sign in to comment.