https://github.com/codytseng/another-nestjs-ws-adapter
another nestjs ws adapter
https://github.com/codytseng/another-nestjs-ws-adapter
adapter nestjs websocket ws
Last synced: about 2 months ago
JSON representation
another nestjs ws adapter
- Host: GitHub
- URL: https://github.com/codytseng/another-nestjs-ws-adapter
- Owner: CodyTseng
- License: mit
- Created: 2024-03-08T08:27:55.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-03-29T19:08:30.000Z (about 1 year ago)
- Last Synced: 2024-05-02T05:20:37.865Z (about 1 year ago)
- Topics: adapter, nestjs, websocket, ws
- Language: TypeScript
- Homepage:
- Size: 134 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Another NestJS WS Adapter
An adapter modified based on the official [`WsAdapter`](https://docs.nestjs.com/websockets/adapter#ws-library).
## Differences from the official `WsAdapter`
- Support any format of message, you can pre-process the message before finding the handler. The official `WsAdapter` only supports messages in the format of `{ "event": string, "data": any }`.
## Installation
```bash
npm install another-nestjs-ws-adapter
```## Usage
```typescript
const app = await NestFactory.create(AppModule);// The default behavior is same as the official `WsAdapter`
const wsAdapter = new WsAdapter(app);// If you want to handle the message in a different format, you can set the message pre-processor like this.
wsAdapter.setMessagePreprocessor((message: any) => {
const [event, ...data] = message;
return { event, data };
});app.useWebSocketAdapter(wsAdapter);
```## API
### `setMessagePreprocessor`
```typescript
type MessagePreprocessor = (message: any, client: WebSocket) => { event: string, data: any } | void;
setMessagePreprocessor(preprocessor: MessagePreprocessor): void;
```Set the message pre-processor. The pre-processor will be called before finding the handler. If the pre-processor returns `void`, the message will be ignored. Otherwise, the message will be processed by the pre-processor and the result should be in the format of `{ event: string, data: any }`. The `client` parameter is the WebSocket client that sends the message.
## License
MIT