Skip to content

Commit

Permalink
update README with examples
Browse files Browse the repository at this point in the history
  • Loading branch information
darthwalsh committed May 22, 2020
1 parent ff1b9cf commit f551a97
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
50 changes: 44 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,53 @@

A drop-in replacement for WebSocket using [Firebase Realtime Database](https://firebase.google.com/docs/database).

## Example
## Examples

A full example showing dynamically switching between FireSocket and WebSocket is in [example/](example/).
An example showing dynamically switching between FireSocket and WebSocket is in [example/](example/).

A [full example](https://github.com/darthwalsh/FireSocketExample) is running at https://firesocketexample.appspot.com/.

### Code changes

TODO(example) example snippet
#### Before with WebSocket

server:

```javascript
const express = require("express");
const ws = require("ws");
const app = express();
const wss = new ws.Server({server});

wss.on("connection", ws => ...
```
client:
```javascript
const ws = new WebSocket(window.location.href.replace("http", "ws"));
ws.addEventListener("open", () => ...
```
#### Using FireSocket
server:
```javascript
const firesocket = require("firesocket");
const express = require("express");
const app = express();
const wss = firesocket.Server.createFromCreds(databaseUrl, {app, firebaseConfig});

wss.on("connection", ws => ...
```
client:
TODO(example) example code showing where to plugin public/secret firebase config
```javascript
const ws = new FireSocket(); // Args are ignored
ws.addEventListener("open", () => ...
```
### Firebase setup
Expand Down Expand Up @@ -78,7 +116,7 @@ TODO(example) example code showing where to plugin public/secret firebase config
- ~~npm badge~~
- [build badges](https://ljvmiranda921.github.io/notebook/2018/12/21/cloud-build-badge/)
- ~~Github status checks on build success~~
- https://medium.com/@Philmod/npm-release-automation-adb970e49066
- ~~https://medium.com/@Philmod/npm-release-automation-adb970e49066~~
- firebase disconnect messages [using onDisconnect](https://firebase.google.com/docs/database/web/offline-capabilities#how-ondisconnect-works)
- Get vs code typing for firesocket.Server working
- Any TODOs left in README
Expand All @@ -91,7 +129,7 @@ TODO(example) example code showing where to plugin public/secret firebase config
## Testing
Like normal, run `npm test` to run all tests. This includes unit tests, and mock tests using the firebase emulator and a local WebSocket server. See [spec/](spec/README.md) for more information.
Run `npm test` to run all tests. This includes unit tests, and mock tests using the firebase emulator and a local WebSocket server. See [spec/](spec/README.md) for more information.
The [example/](example/) can be useful for manually debugging changes.
Expand Down
2 changes: 2 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class Server {
}

// TODO test?
// TODO could databaseUrl be inferred from projectId?
// TODO just make this the FireSocket.Server constructor?
/**
* helper constructor to avoid using firebase directly
* use application default credentials (https://firebase.google.com/docs/admin/setup#initialize-sdk)
Expand Down

0 comments on commit f551a97

Please sign in to comment.