Chat server using WebSockets with Nest framework.
npm install
# development
npm run start
# watch mode
npm run start:dev
npm i --save @nestjs/websockets @nestjs/platform-socket.io ngx-socket-io
npm i --save-dev @types/socket.io
-
We add
@WebSocketServer() server;
inside of our ChatWebsocketGateway to attaches a native Web Socket Server to our propertyserver
which is real-time, bidirectional and event-based communication between the client and the server -
To handle the connection and disconnection at our websocket server we need implement interfaces
OnGatewayConnection
andOnGatewayDisconnect
. -
We use decorator
@SubscribeMessage('exchanges')
on the method that handles our messagin rules onexchanges
events,
-
/api/v1/rooms
roomId: the room id (room name) creatorUsername: the username with creats the room
curl -X POST 'http://localhost:3000/api/v1/rooms' \ --data-raw '{ "roomId": "3XX", "creatorUsername": "Dhoni" }'
- Invalid body:
{ "statusCode": 400, "message": [ "roomId should not be empty", "creatorUsername should not be empty" ], "error": "Bad Request" }
- Existing room id:
{ "code": "room.conflict", "message": "Room with 'exmple-room' already exists" }
-
/api/v1/rooms/{roomId}/messages
roomId: the room id
curl -X GET 'http://localhost:3000/api/v1/rooms/123/messages'
- Invalid room id (ex: not found or closed):
{ "code": "access-forbidden", "message": "The access is forbidden" }
-
/api/v1/rooms/{roomId}
roomId: the room id
curl -X DELETE http://localhost:3000/api/v1/rooms/123
- Invalid room id (ex: not found or closed):
{ "code": "room.not-found", "message": "Room with '3XX' not found" }