跳至主要内容

[Chat App] Interface

Here are the interfaces with the backend server.

HTTP API endpoints

1. GET /users

Get the users you can create a new room with.


Payload:None
Response

[
{
"id": "user_123",
"name": "Alice",
"avatar": "https://example.com/avatar/alice.png"
},
{
"id": "user_456",
"name": "Bob",
"avatar": "https://example.com/avatar/bob.png"
}
]


2. POST /users

Create a new user

Payload

{
"name": "Alice",
"avatar": "https://example.com/avatar/alice.png"
}

Response

{
"id": "user_123",
"name": "Alice",
"avatar": "https://example.com/avatar/alice.png"
}


3. GET /rooms/{room_id}/messages

Get the messages of the room


Payload:None (only room_id param)
Response

[
{
"roomId": "room_456",
"messageId": "msg_001",
"content": "Hello!",
"senderId": "user_123",
"createdAt": "2024-06-16T12:34:56.789Z",
"status": "success"
},
{
"roomId": "room_456",
"messageId": "msg_002",
"content": "Hi!",
"senderId": "user_456",
"createdAt": "2024-06-16T12:35:12.000Z",
"status": "success"
}
]


WebSocket action endpoints


4. create_room

The reason we use websocket is to notify a new room to users which is not in the room but online, which can be replaced by HTTP API + SSE to notify other not in-room users.


Payload

{
"action": "create_room",
"participants": ["user_123", "user_456"]
}

Server Response(broadcast to participants):

{
"type": "room_created",
"room": {
"id": "room_456",
"participants": [
{ "id": "user_123", "name": "Alice" },
{ "id": "user_456", "name": "Bob" }
],
"createdAt": "2024-06-16T12:36:00.000Z"
}
}


5. create_message

Create a new message by a user to notify other users in the room.


Payload

{
"action": "create_message",
"roomId": "room_456",
"messageId": "msg_003", // client-generated UUID
"content": "How are you?",
"senderId": "user_123",
"createdAt": "2024-06-16T12:36:10.000Z"
}

Server Response(broadcast to all users in the room):

{
"type": "message_created",
"message": {
"roomId": "room_456",
"messageId": "msg_003",
"content": "How are you?",
"senderId": "user_123",
"createdAt": "2024-06-16T12:36:10.000Z",
"status": "success"
}
}


Reference

  • GreatFrontend - Chat App