@@ -33,11 +33,12 @@ After registering this plugin, you can choose on which routes the WS server will
3333
3434const fastify = require (' fastify' )()
3535fastify .register (require (' @fastify/websocket' ))
36-
37- fastify .get (' /' , { websocket: true }, (connection /* SocketStream */ , req /* FastifyRequest */ ) => {
38- connection .socket .on (' message' , message => {
39- // message.toString() === 'hi from client'
40- connection .socket .send (' hi from server' )
36+ fastify .register (async function (fastify ) {
37+ fastify .get (' /' , { websocket: true }, (connection /* SocketStream */ , req /* FastifyRequest */ ) => {
38+ connection .socket .on (' message' , message => {
39+ // message.toString() === 'hi from client'
40+ connection .socket .send (' hi from server' )
41+ })
4142 })
4243})
4344
@@ -62,18 +63,19 @@ fastify.register(require('@fastify/websocket'), {
6263 options: { maxPayload: 1048576 }
6364})
6465
65-
66- fastify .get (' /*' , { websocket: true }, (connection /* SocketStream */ , req /* FastifyRequest */ ) => {
67- connection .socket .on (' message' , message => {
68- // message.toString() === 'hi from client'
69- connection .socket .send (' hi from wildcard route' )
66+ fastify .register (async function (fastify ) {
67+ fastify .get (' /*' , { websocket: true }, (connection /* SocketStream */ , req /* FastifyRequest */ ) => {
68+ connection .socket .on (' message' , message => {
69+ // message.toString() === 'hi from client'
70+ connection .socket .send (' hi from wildcard route' )
71+ })
7072 })
71- })
7273
73- fastify .get (' /' , { websocket: true }, (connection /* SocketStream */ , req /* FastifyRequest */ ) => {
74- connection .socket .on (' message' , message => {
75- // message.toString() === 'hi from client'
76- connection .socket .send (' hi from server' )
74+ fastify .get (' /' , { websocket: true }, (connection /* SocketStream */ , req /* FastifyRequest */ ) => {
75+ connection .socket .on (' message' , message => {
76+ // message.toString() === 'hi from client'
77+ connection .socket .send (' hi from server' )
78+ })
7779 })
7880})
7981
@@ -127,11 +129,10 @@ This plugin uses the same router as the `fastify` instance, this has a few impli
127129- When using ` @fastify/websocket ` , it needs to be registered before all routes in order to be able to intercept websocket connections to existing routes and close the connection on non-websocket routes.
128130
129131``` js
130- ' use strict'
131-
132- const fastify = require (' fastify' )()
132+ import Fastify from ' fastify'
133133
134- fastify .register (require (' @fastify/websocket' ))
134+ const fastify = Fastify ()
135+ await fastify .register (require (' @fastify/websocket' ))
135136
136137fastify .get (' /' , { websocket: true }, function wsHandler (connection , req ) {
137138 // bound to fastify server
@@ -143,12 +144,7 @@ fastify.get('/', { websocket: true }, function wsHandler (connection, req) {
143144 })
144145})
145146
146- fastify .listen (3000 , err => {
147- if (err) {
148- fastify .log .error (err)
149- process .exit (1 )
150- }
151- })
147+ await fastify .listen ({ port: 3000 })
152148```
153149
154150If 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.
@@ -167,22 +163,24 @@ fastify.register(require('@fastify/websocket'), {
167163 options: { maxPayload: 1048576 }
168164})
169165
170- fastify .route ({
171- method: ' GET' ,
172- url: ' /hello' ,
173- handler : (req , reply ) => {
174- // this will handle http requests
175- reply .send ({ hello: ' world' })
176- },
177- wsHandler : (conn , req ) => {
178- // this will handle websockets connections
179- conn .setEncoding (' utf8' )
180- conn .write (' hello client' )
181-
182- conn .once (' data' , chunk => {
183- conn .end ()
184- })
185- }
166+ fastify .register (async function () {
167+ fastify .route ({
168+ method: ' GET' ,
169+ url: ' /hello' ,
170+ handler : (req , reply ) => {
171+ // this will handle http requests
172+ reply .send ({ hello: ' world' })
173+ },
174+ wsHandler : (conn , req ) => {
175+ // this will handle websockets connections
176+ conn .setEncoding (' utf8' )
177+ conn .write (' hello client' )
178+
179+ conn .once (' data' , chunk => {
180+ conn .end ()
181+ })
182+ }
183+ })
186184})
187185
188186fastify .listen (3000 , err => {
0 commit comments