An open API service indexing awesome lists of open source software.

https://github.com/tashuo/nest-websocket

nest websocket example
https://github.com/tashuo/nest-websocket

jwt nest nestjs nodejs socket-io typeorm websocket

Last synced: 4 months ago
JSON representation

nest websocket example

Awesome Lists containing this project

README

          


Nest Logo

[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
[circleci-url]: https://circleci.com/gh/nestjs/nest

A progressive Node.js framework for building efficient and scalable server-side applications.



NPM Version
Package License
NPM Downloads
CircleCI
Coverage
Discord
Backers on Open Collective
Sponsors on Open Collective

Support us


## Description
A nest websocket example with auth.

实现了websocket连接鉴权、心跳保活、私聊和系统主动推送通知等逻辑

## Installation

```bash
# install
$ pnpm up -latest

# .env
$ cp .env.example .env # modify the config with yours

# run migration
$ pnpm typeorm:run-migrations
```

## Running the app

```bash
# development
$ pnpm start

# watch mode
$ pnpm start:dev
```

## Test
### login to get jwt
*the test users Jim and John are auto generated on nest start, look at [OnApplicationBootstrap](https://github.com/tashuo/nest-websocket/blob/master/src/modules/user/user.module.ts)*

```bash
$ curl -X POST \
'127.0.0.1:3000/user/login' \
--header 'Content-Type: application/json' \
--data-raw '{
"username": "Jim",
"password": "123456"
}'

$ curl -X POST \
'127.0.0.1:3000/user/login' \
--header 'Content-Type: application/json' \
--data-raw '{
"username": "John",
"password": "123456"
}'
```

### test with Postman
#### connect to ws server with jwt token

[![ixBr6Q.md.png](https://i.328888.xyz/2023/04/14/ixBr6Q.md.png)](https://imgloc.com/i/ixBr6Q)

#### add chat event listener
[![ixBGld.md.png](https://i.328888.xyz/2023/04/14/ixBGld.md.png)](https://imgloc.com/i/ixBGld)

#### send message directly with ws
[![ixBYc5.md.png](https://i.328888.xyz/2023/04/14/ixBYc5.md.png)](https://imgloc.com/i/ixBYc5)

[![ixXVGH.md.png](https://i.328888.xyz/2023/04/14/ixXVGH.md.png)](https://imgloc.com/i/ixXVGH)

#### send message with http api
[![ixXO7d.md.png](https://i.328888.xyz/2023/04/14/ixXO7d.md.png)](https://imgloc.com/i/ixXO7d)

[![ixXev5.md.png](https://i.328888.xyz/2023/04/14/ixXev5.md.png)](https://imgloc.com/i/ixXev5)

### test heartbeat
#### normal
[![ixgprJ.md.png](https://i.328888.xyz/2023/04/14/ixgprJ.md.png)](https://imgloc.com/i/ixgprJ)

#### timeout
[![ixgdDz.md.png](https://i.328888.xyz/2023/04/14/ixgdDz.md.png)](https://imgloc.com/i/ixgdDz)

## License

Nest is [MIT licensed](LICENSE).