Skip to main content

Chat Operations

This section covers GraphQL operations related to the chat functionality.

Queries​

All Channels​

Retrieves all public live stream channels.

query AllChannels {
allChannels {
id
type
name
avatar
}
}

Response:

{
"data": {
"allChannels": [
{
"id": "channel-id-1",
"type": "livestream",
"name": "General Chat",
"avatar": "https://example.com/channel-avatar.jpg"
},
{
"id": "channel-id-2",
"type": "livestream",
"name": "News",
"avatar": null
}
]
}
}

All User Chats​

Retrieves all chats for the currently authenticated user with pagination.

query AllUserChats(
$limit: Int
$offset: Int
$order: SortOrder
$orderBy: ChatStreamChannelOrderBy
) {
allUserChats(
limit: $limit
offset: $offset
order: $order
orderBy: $orderBy
) {
data {
id
}
limit
offset
}
}

Input Parameters:

{
"limit": 10,
"offset": 0,
"order": "DESC",
"orderBy": "LAST_MESSAGE_AT"
}

Response:

{
"data": {
"allUserChats": {
"data": [
{
"id": "chat-id-1"
},
{
"id": "chat-id-2"
}
],
"limit": 10,
"offset": 0
}
}
}

Mutations​

Create Chat with Other Users​

Creates a new chat channel with specified users.

mutation CreateChatWithOtherUsers($input: CreateChatWithUsersInput!) {
createChatWithOtherUsers(input: $input)
}

Input Parameters:

{
"input": {
"userIds": ["user-id-1", "user-id-2"]
}
}

Response:

{
"data": {
"createChatWithOtherUsers": true
}
}

Chat Interaction​

After retrieving chat channels and creating chats, you'll need to use the Stream Chat client library to interact with the chats. Use the streamChatToken query to get a token for the Stream Chat API.

Stream Chat Integration​

  1. Get Stream Chat Token
query StreamChatToken {
streamChatToken
}
  1. Initialize Stream Chat Client

Use the token to initialize the Stream Chat client in your application:

// Example using JavaScript Stream Chat client
const chatClient = StreamChat.getInstance(apiKey);
await chatClient.connectUser({ id: userId }, streamChatToken);
  1. Access Chat Channels

After connecting to Stream Chat, you can access the channels retrieved from the GraphQL API:

// Example using JavaScript Stream Chat client
const channel = chatClient.channel('livestream', channelId);
await channel.watch();

Channel Types​

  • livestream: Public channel visible to all users
  • messaging: Private channel between specific users

Input Types​

ChatStreamChannelsArgs​

FieldTypeDescriptionDefault
offsetIntNumber of items to skip0
limitIntNumber of items to take10
orderSortOrderSort order (ASC or DESC)DESC
orderByChatStreamChannelOrderByField to sort byLAST_MESSAGE_AT

CreateChatWithUsersInput​

FieldTypeDescriptionRequired
userIds[String!]List of valid MongoDB user IDs to create chat withYes

ChatStreamChannelOrderBy enum values​

  • CREATED_AT: Sort by channel creation time
  • LAST_MESSAGE_AT: Sort by time of the last message (default)
  • LAST_UPDATED: Sort by channel update time