Skip to content

Commit 03b5297

Browse files
Merge branch 'CodyTseng-feat-ws-message-preprocessor' into 11.0.0
2 parents 93a9215 + c022686 commit 03b5297

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

content/websockets/adapter.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ app.useWebSocketAdapter(new WsAdapter(app));
9494

9595
> info **Hint** The `WsAdapter` is imported from `@nestjs/platform-ws`.
9696
97+
The `wsAdapter` is designed to handle messages in the `{{ '{' }} event: string, data: any {{ '}' }}` format. If you need to receive and process messages in a different format, you'll need to configure a message parser to convert them into this required format.
98+
99+
```typescript
100+
const wsAdapter = new WsAdapter(app, {
101+
// To handle messages in the [event, data] format
102+
messageParser: (data) => {
103+
const [event, payload] = JSON.parse(data.toString());
104+
return { event, data: payload };
105+
},
106+
});
107+
```
108+
109+
Alternatively, you can configure the message parser after the adapter is created by using the `setMessageParser` method.
110+
97111
#### Advanced (custom adapter)
98112

99113
For demonstration purposes, we are going to integrate the [ws](https://github.com/websockets/ws) library manually. As mentioned, the adapter for this library is already created and is exposed from the `@nestjs/platform-ws` package as a `WsAdapter` class. Here is how the simplified implementation could potentially look like:

0 commit comments

Comments
 (0)