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
`WebSocketClient` implements a [WebSocket](https://github.com/websockets/ws/blob/master/doc/ws.md#class-websocket). So if you are familiar with it, you are ready to go.
147
+
148
+
```ts
149
+
import { Injectable } from'@nestjs/common'
150
+
import {
151
+
InjectWebSocketProvider,
152
+
WebSocketClient,
153
+
OnOpen,
154
+
OnMessage,
155
+
} from'nestjs-websocket';
156
+
157
+
@Injectable()
158
+
classTestService {
159
+
private data:Record<any, any> = {}
160
+
161
+
constructor(
162
+
@InjectWebSocketProvider()
163
+
privatereadonlyws:WebSocketClient,
164
+
) {}
165
+
166
+
@OnOpen()
167
+
onOpen() {
168
+
this.ws.send(JSON.stringify(eventData))
169
+
}
170
+
171
+
@OnMessage()
172
+
message(data:WebSocketClient.Data) {
173
+
this.data=JSON.parse(data.toString())
174
+
}
175
+
176
+
async getData():Promise<Record<any, any>> {
177
+
returnthis.data
178
+
}
179
+
}
180
+
```
181
+
182
+
## Websocket Events
183
+
184
+
### EventListener
185
+
186
+
`@EventListener` decorator will handle any event emitted from websocket server.
console.log(`The server response ${response} is not the expected one.`)
210
+
}
211
+
212
+
@EventListener('upgrade')
213
+
upgrade(response:IncomingMessage) {
214
+
console.log(`Response headers ${response} are received from the server as part of the handshake.`)
215
+
}
216
+
}
217
+
```
218
+
219
+
### OnOpen
220
+
221
+
`@OnOpen` is a shortcut for `@EventListener('open')`. Event emitted when the connection is established.
222
+
223
+
```ts
224
+
import { Injectable } from'@nestjs/common'
225
+
import {
226
+
OnOpen
227
+
} from'nestjs-websocket';
228
+
229
+
@Injectable()
230
+
classTestService {
231
+
@OnOpen()
232
+
open() {
233
+
console.log('The connection is established.')
234
+
}
235
+
}
236
+
```
237
+
238
+
### OnClose
239
+
240
+
`@OnClose` is a shortcut for `@EventListener('close')` Event emitted when the connection is closed. `code` property is a numeric value for status code explaining why the connection has been closed. `reason` is a Buffer containing a human-readable string explaining why the connection has been closed.
241
+
242
+
```ts
243
+
import { Injectable } from'@nestjs/common'
244
+
import {
245
+
OnClose
246
+
} from'nestjs-websocket';
247
+
248
+
@Injectable()
249
+
classTestService {
250
+
@OnClose()
251
+
close(code:number, reason:string) {
252
+
console.log(`The connection is closed. Reason: ${code} - ${reason}`)
253
+
}
254
+
}
255
+
```
256
+
257
+
### OnError
258
+
259
+
`@OnError` is a shortcut for `@EventListener('error')`. Event emitted when an error occurs. Errors may have a [.code](https://github.com/websockets/ws/blob/HEAD/doc/ws.md#ws-error-codes) property.
260
+
261
+
```ts
262
+
import { Injectable } from'@nestjs/common'
263
+
import {
264
+
OnError
265
+
} from'nestjs-websocket';
266
+
267
+
@Injectable()
268
+
classTestService {
269
+
@OnError()
270
+
error(err:Error) {
271
+
console.log(`An error occurs: ${err}`)
272
+
}
273
+
}
274
+
```
275
+
276
+
### OnMessage
277
+
278
+
`@OnMessage` is a shortcut for `@EventListener('message')`. Event emitted when a message is received. `data` is the message content.
## Testing a class that uses @InjectWebSocketProvider
75
296
76
297
This package exposes a `getWebSocketToken()` function that returns a prepared injection token based on the provided context.
77
-
Using this token, you can easily provide a mock implementation of the [WS](https://github.com/websockets/ws) using any of the standard custom provider techniques, including useClass, useValue, and useFactory.
298
+
Using this token, you can easily provide a mock implementation of the [ws](https://github.com/websockets/ws) using any of the standard custom provider techniques, including useClass, useValue, and useFactory.
0 commit comments