1 API GET messages
lucas edited this page 2025-12-02 12:12:20 +01:00

The messages API endpoint is likely the most complex one - it needs pagination (random link: why pagination ) and it ideally returns data in a way we can immediately use it to show to the user, including the username and optionally maybe a profile picture of the user who sent it.

To solve this in shinyspace-chat, we'll return two lists, one containing all the messages in the requested range (going with cursor-based pagination, since postgres also supports that well) and one containing details on all the users that are included in the list.

Quick llm-generated example of what that might look like: { "messages": [ {"id": 1, "user_id": "u_123", "content": "Hello", "created_at": "..."}, {"id": 2, "user_id": "u_456", "content": "Hi there", "created_at": "..."}, {"id": 3, "user_id": "u_123", "content": "How are you?", "created_at": "..."} ], "users": { "u_123": {"id": "u_123", "username": "alice", "avatar_url": "..."}, "u_456": {"id": "u_456", "username": "bob", "avatar_url": "..."} } }

note that this uses an array for messages "[ ]" because these are commonly accessed sequentially, but a dictionary with keys for users, this makes it faster to access info for a specific user when we need it in the frontend.