Skip to content

Commit 4de04f1

Browse files
committed
docs(ws): add usage for setMessagePreprocessor method
1 parent 265cc58 commit 4de04f1

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

content/websockets/adapter.md

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

9595
> info **Hint** The `WsAdapter` is imported from `@nestjs/platform-ws`.
9696
97+
The `wsAdapter` can only route messages in the `{ event: string, data: any }` format. If you need to receive and process messages in other formats, you should set up an appropriate message preprocessor to convert the messages into the `{ event: string, data: any }` format.
98+
99+
```typescript
100+
const wsAdapter = new WsAdapter(app);
101+
102+
// to process [event, ...data] format messages
103+
wsAdapter.setMessagePreprocessor((message: any) => {
104+
const [event, ...data] = message;
105+
return { event, data };
106+
});
107+
```
108+
97109
#### Advanced (custom adapter)
98110

99111
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)