Skip to content

Commit ffa34ac

Browse files
committed
Initial implementation
1 parent fbbfa7b commit ffa34ac

File tree

6 files changed

+3537
-1
lines changed

6 files changed

+3537
-1
lines changed

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: node_js
2+
node_js:
3+
- "8"
4+
- "7"
5+
- "6"
6+
- "4"
7+
notifications:
8+
email:
9+
on_success: never
10+
on_failure: always

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,32 @@
11
# fastify-websocket
2-
basic websocket support for fastify
2+
3+
WebSocket support for [Fastify](https://github.com/fastify/fastify).
4+
Built upon [websocket-stream](http://npm.im/websocket-stream).
5+
6+
## Example
7+
8+
```js
9+
'use strict'
10+
11+
const fastify = require('fastify')()
12+
13+
fastify.register(require('fastify-websocket'), { handle })
14+
15+
function handle (conn) {
16+
conn.pipe(conn) // creates a echo server
17+
}
18+
19+
fastify.listen(0)
20+
```
21+
22+
## TODO
23+
24+
* [ ] Support Hooks?
25+
26+
## Acknowledgements
27+
28+
This project is kindly sponsored by [nearForm](http://nearform.com).
29+
30+
## License
31+
32+
Licensed under [MIT](./LICENSE).

index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
const fp = require('fastify-plugin')
4+
const websocket = require('websocket-stream')
5+
6+
module.exports = fp(function (fastify, opts, next) {
7+
var handle = opts.handle
8+
9+
if (typeof handle !== 'function') {
10+
return next(new Error('invalid handle function'))
11+
}
12+
13+
var wss = websocket.createServer({
14+
server: fastify.server
15+
}, handle)
16+
17+
fastify.decorate('websocketServer', wss)
18+
19+
next()
20+
})

0 commit comments

Comments
 (0)