|
| 1 | +# NestJS-Websocket |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/nestjs-websocket) |
| 4 | +[](https://circleci.com/gh/blockcoders/nestjs-websocket/tree/main) |
| 5 | +[](https://coveralls.io/github/blockcoders/nestjs-websocket?branch=main) |
| 6 | +[](https://snyk.io/test/github/blockcoders/nestjs-websocket) |
| 7 | +[](https://img.shields.io/badge/platforms-Express%20%26%20Fastify-green) |
| 8 | + |
| 9 | +Websocket utility for NestJS based on [WS](https://www.npmjs.com/package/ws) |
| 10 | + |
| 11 | +## Install |
| 12 | + |
| 13 | +```sh |
| 14 | +npm i nestjs-websocket |
| 15 | +``` |
| 16 | + |
| 17 | +## Register module |
| 18 | + |
| 19 | +### Configuration params |
| 20 | + |
| 21 | +The url param that websocket module expects should be a string, for example: |
| 22 | + |
| 23 | +```ts |
| 24 | +{ |
| 25 | + url: 'ws://localhost:3000', |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +### Synchronous configuration |
| 30 | + |
| 31 | +Use `WebSocketModule.forRoot` method with [String param](#configuration-params): |
| 32 | + |
| 33 | +```ts |
| 34 | +import { WebSocketModule } from 'nestjs-websocket' |
| 35 | + |
| 36 | +@Module({ |
| 37 | + imports: [ |
| 38 | + WebSocketModule.forRoot({ |
| 39 | + url: 'ws://localhost:3000', |
| 40 | + }), |
| 41 | + ], |
| 42 | + ... |
| 43 | +}) |
| 44 | +class MyModule {} |
| 45 | +``` |
| 46 | + |
| 47 | +### Asynchronous configuration |
| 48 | + |
| 49 | +`WebSocketModule.forRootAsync` allows you, for example, inject `ConfigService` to use it in Nest `useFactory` method. |
| 50 | + |
| 51 | +`useFactory` should return an object with [String param](#configuration-params). |
| 52 | + |
| 53 | +```ts |
| 54 | +import { WebSocketModule } from 'nestjs-websocket' |
| 55 | + |
| 56 | +@Injectable() |
| 57 | +@Module({ |
| 58 | + imports: [ |
| 59 | + WebSocketModule.forRootAsync({ |
| 60 | + imports: [ConfigModule], |
| 61 | + inject: [ConfigService], |
| 62 | + useFactory: (config: ConfigService) => { |
| 63 | + return { |
| 64 | + url: 'ws://localhost:3000' |
| 65 | + } |
| 66 | + }, |
| 67 | + }), |
| 68 | + ], |
| 69 | + ... |
| 70 | +}) |
| 71 | +class MyModule {} |
| 72 | +``` |
| 73 | + |
| 74 | +## Testing a class that uses @InjectWebSocketProvider |
| 75 | + |
| 76 | +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. |
| 78 | + |
| 79 | +```ts |
| 80 | +const module: TestingModule = await Test.createTestingModule({ |
| 81 | + providers: [ |
| 82 | + MyService, |
| 83 | + { |
| 84 | + provide: getWebSocketToken(), |
| 85 | + useValue: mockProvider, |
| 86 | + }, |
| 87 | + ], |
| 88 | +}).compile(); |
| 89 | +``` |
| 90 | + |
| 91 | +## Change Log |
| 92 | + |
| 93 | +See [Changelog](CHANGELOG.md) for more information. |
| 94 | + |
| 95 | +## Contributing |
| 96 | + |
| 97 | +Contributions welcome! See [Contributing](CONTRIBUTING.md). |
| 98 | + |
| 99 | +## Authors |
| 100 | + |
| 101 | +- [**Jose Ramirez**](https://github.com/jarcodallo), [Twitter](https://twitter.com/jarcodallo), [NPM](https://www.npmjs.com/~jarcodallo) |
| 102 | +- [**Ana Riera**](https://github.com/AnnRiera) |
| 103 | + |
| 104 | +## License |
| 105 | + |
| 106 | +Licensed under the Apache 2.0 - see the [LICENSE](LICENSE) file for details. |
0 commit comments