/nest-websocket

nest websocket example

Primary LanguageTypeScript

Nest Logo

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

# install
$ pnpm up -latest

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

# run migration
$ pnpm typeorm:run-migrations

Running the app

# 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

$ 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

add chat event listener

ixBGld.md.png

send message directly with ws

ixBYc5.md.png

ixXVGH.md.png

send message with http api

ixXO7d.md.png

ixXev5.md.png

test heartbeat

normal

ixgprJ.md.png

timeout

ixgdDz.md.png

License

Nest is MIT licensed.