You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
All you need to do is to add it to your project with `register` and you are done!
16
+
There are two possible ways of using this plugin: with a **global handler** or with a **per route handler**.
17
+
18
+
### Global handler
16
19
17
-
### Example
20
+
All you need to do is to add it to your project with `register` and pass handle function. You are done!
18
21
19
22
```js
20
23
'use strict'
21
24
22
25
constfastify=require('fastify')()
23
26
24
-
constwssOptions= {
25
-
maxPayload:1048576, // we set the maximum allowed messages size to 1 MiB (1024 bytes * 1024 bytes)
26
-
path:'/fastify', // we accept only connections matching this path e.g.: ws://localhost:3000/fastify
27
-
verifyClient:function (info, next) {
28
-
if (info.req.headers['x-fastify-header'] !=='fastify is awesome !') {
29
-
returnnext(false) // the connection is not allowed
27
+
fastify.register(require('fastify-websocket'), {
28
+
handle,
29
+
options: {
30
+
maxPayload:1048576, // we set the maximum allowed messages size to 1 MiB (1024 bytes * 1024 bytes)
31
+
path:'/fastify', // we accept only connections matching this path e.g.: ws://localhost:3000/fastify
32
+
verifyClient:function (info, next) {
33
+
if (info.req.headers['x-fastify-header'] !=='fastify is awesome !') {
34
+
returnnext(false) // the connection is not allowed
35
+
}
36
+
next(true) // the connection is allowed
30
37
}
38
+
}
39
+
})
40
+
41
+
functionhandle (conn) {
42
+
conn.pipe(conn) // creates an echo server
43
+
}
44
+
45
+
fastify.listen(3000, err=> {
46
+
if (err) {
47
+
fastify.log.error(err)
48
+
process.exit(1)
49
+
}
50
+
})
51
+
```
31
52
32
-
next(true) // the connection is allowed
53
+
### Per route handler
54
+
55
+
After registring this plugin, you can choose on which routes the WS server will respond. This could be achieved adding `websocket: true` property to `routeOptions` on a fastify's `.get` route. In this case two arguments will be passed to the handler: socket connection and the original `http.IncomingMessage` (instead of the usual fastify's request and reply objects).
In this case there won't be any global handler, so it will respond with a 404 error on every unregistered route, closing the incoming upgrade connection requests.
80
+
81
+
However you can still pass a default global handler, that will be used as default handler.
If you need to handle both HTTP requests and incoming socket connections on the same route, you can still do it using the [full declaration syntax](https://www.fastify.io/docs/latest/Routes/#full-declaration), adding a `wsHandler` property.
113
+
114
+
```js
115
+
'use strict'
116
+
117
+
constfastify=require('fastify')()
118
+
41
119
functionhandle (conn) {
42
120
conn.pipe(conn) // creates an echo server
43
121
}
44
122
45
-
fastify.listen(3000, (err) => {
123
+
fastify.register(require('fastify-websocket'), {
124
+
handle,
125
+
options: { maxPayload:1048576 }
126
+
})
127
+
128
+
fastify.route({
129
+
method:'GET',
130
+
url:'/hello',
131
+
handler: (req, reply) => {
132
+
// this will handle http requests
133
+
reply.send({ hello:'world' })
134
+
},
135
+
wsHandler: (conn, req) => {
136
+
// this will handle websockets connections
137
+
conn.setEncoding('utf8')
138
+
conn.write('hello client')
139
+
140
+
conn.once('data', chunk=> {
141
+
conn.end()
142
+
})
143
+
}
144
+
})
145
+
146
+
fastify.listen(3000, err=> {
46
147
if (err) {
47
-
fastify.log.error(err);
48
-
process.exit(1);
148
+
fastify.log.error(err)
149
+
process.exit(1)
49
150
}
50
151
})
51
152
```
52
153
53
154
## Options :
155
+
54
156
`fastify-websocket` accept the same options as [`websocket-stream`](https://github.com/maxogden/websocket-stream#options) and as [`ws`](https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback) :
55
157
56
158
-`objectMode` - Send each chunk on its own, and do not try to pack them in a single websocket frame.
@@ -70,10 +172,6 @@ For more informations you can check [`ws` options documentation](https://github.
70
172
71
173
_**NB:** By default if you do not provide a `server` option `fastify-websocket` will bind your websocket server instance to the scoped `fastify` instance._
72
174
73
-
## TODO
74
-
75
-
*[ ] Support Hooks?
76
-
77
175
## Acknowledgements
78
176
79
177
This project is kindly sponsored by [nearForm](http://nearform.com).
0 commit comments