For AI agents: Find a documentation index at https://talkjs.com/llms.txt (full content at https://talkjs.com/llms-full.txt). Get a markdown version of any page by appending .md to its URL path.

Filters

Filter list subscriptions to show only matching data.

View as Markdown

Filters allow you to only show specific data inside of a list subscription. For example, only showing conversations with certain custom data or only showing messages from certain people.

Pass a filter to session.subscribeConversations or conversation.subscribeMessages to subscribe to matching items.

type ConversationFilter

Passed to Session.subscribeConversations, specifying which conversations should be returned in the subscription.

Either a SimpleConversationFilter or a CompoundFilter OR-ing multiple simple filters together.

Example 1
A simple conversation filter that only includes conversations with the subject "cats!"
{ subject: ["==", "cats!"] }
Example 2
A compound conversation filter that includes conversations with messages, and empty conversations with the subject "cats!" ```json ["any", [ { hasMessages: true }, { subject: ["==", "cats!"] } ]]

interface SimpleConversationFilter

Allows you to filter conversations in a subscription, only showing those that match this filter.

Multiple conversation filters can be OR-ed together using CompoundFilter.

Example
Only include conversations with unread messages
session.subscribeConversations(onSnapshot, {
  filter: { isUnread: true }
});

Properties

access (optional)
: StringFilter<"ReadWrite" | "Read">

Only select conversations that the current user as specific access to.

Must be an 2-element array of [operator, operand] structure. Valid operators are: "==", "!=", "oneOf", and "!oneOf".

The operand must be either a string (one of "ReadWrite" or "Read") or an array of strings (for the oneOf operators).

Example
Hide conversations that the user cannot send messages to
{ access: ["!=", "ReadWrite"] }
createdAt (optional)
: NumberFilter

Only select conversations that have been created in a particular time interval.

Must be an 2-element array of [operator, operand] structure. Valid operators are: ">", "<", ">=", "<=", "between", and "!between".

The operand must be either a number or a 2-element array of numbers (for the between operators).

Example
Only show conversations that were created after the UNIX timestamp 1679298371586
{ createdAt: [">", 1679298371586] }
custom (optional)
: CustomFilter

Only select conversations that have particular custom fields set to particular values.

Every key must correspond to a key in the custom conversation data that you set (by passing custom to ConversationBuilder​.setAttributes). It is not necessary for all conversations to have these keys.

Each value must be one of the following:

A string, equal to "exists" or "!exists"

A 2-element array of [operator, operand] structure. The operand must be either a string or an array of strings (for the oneOf operators). Valid operators are: "==", "!=", "oneOf", and "!oneOf".

Example 1
Only show messages that have no custom category set
{ custom: { category: "!exists" } }
Example 2
Only show messages that have the custom category "shoes"
{ custom: { category: ["==", "shoes"] } }
Example 3
Only show messages that have a 'topic' of either "inquiry" or "reservation"
{
  custom: {
    topic: ["oneOf", ["inquiry", "reservation"]]
  }
}
Example 4
Only show messages about shoes that are marked visible
{
  custom: {
    category: ["==", "shoes"],
    visibility: ["==", "visible"]
  }
}
hasMessages (optional)
: boolean

Only select conversations that have, or do not have messages.

Example 1
Only show conversations that have at least one message
{ hasMessages: true }
Example 2
Only show empty conversations
{ hasMessages: false }
isUnread (optional)
: boolean

Set this field to only select conversations that have, or don't have any, unread messages.

joinedAt (optional)
: NumberFilter

Only select conversations that the current user joined in a particular time interval.

Must be an 2-element array of [operator, operand] structure. Valid operators are: ">", "<", ">=", "<=", "between", and "!between".

The operand must be either a number or a 2-element array of numbers (for the between operators).

Example
Only show conversations joined after the UNIX timestamp 1679298371586
{ joinedAt: [">", 1679298371586] }
lastActivityAt (optional)
: NumberFilter

Only select conversations that have the last message sent in a particular time interval.

If the conversation has no messages, this falls back to using joinedAt instead. This matches the default sort order for conversations in a conversation list.

Must be an 2-element array of [operator, operand] structure. Valid operators are: ">", "<", ">=", "<=", "between", and "!between".

The operand must be either a number or a 2-element array of numbers (for the between operators).

Example
Only show conversations that had messages sent after the UNIX timestamp 1679298371586
{ lastActivityAt: [">", 1679298371586] }
subject (optional)
: StringFilter<string | null>

Only select conversations that have the subject set to particular values.

Must be an 2-element array of [operator, operand] structure. Valid operators are: "==", "!=", "oneOf", and "!oneOf".

The operand must be either a string or an array of strings (for the oneOf operators).

Example
Only show conversations with "Black leather boots" or "Hair Wax 5 Gallons" as the subject
{ subject: ["oneOf", ["Black leather boots", "Hair Wax 5 Gallons"]] }

type MessageFilter

Specifies which messages ConversationRef​.subscribeMessages returns.

Pass a SimpleMessageFilter to match one set of conditions, or a CompoundFilter to match any of several simple filters.

Example 1
Only include messages marked as announcements
{ "custom": { "category": ["==", "announcement"] } }
Example 2
Include system messages and messages marked as announcements
["any", [
  { "type": ["==", "SystemMessage"] },
  { "custom": { "category": ["==", "announcement"] } }
]]

interface SimpleMessageFilter

Selects messages that match all the fields in this filter.

Use a CompoundFilter to include messages that match any of several simple filters.

Example
Subscribe to system messages
conversation.subscribeMessages({
  filter: { type: ["==", "SystemMessage"] }
}, onSnapshot);

Properties

type (optional)
: StringFilter<"UserMessage" | "SystemMessage">

Only show messages of a given type (system message or user message)

Example
Only show system messages
{ type: ["==", "SystemMessage"] }
createdAt (optional)
: NumberFilter

Only select messages that have been sent in a particular time interval.

Must be an 2-element array of [operator, operand] structure. Valid operators are: ">", "<", ">=", "<=", "between", and "!between".

The operand must be either a number or a 2-element array of numbers (for the between operators).

Example
Only show messages sent after the UNIX timestamp 1679298371586
{ createdAt: [">", 1679298371586] }
custom (optional)
: CustomFilter

Only select messages that have particular custom fields set to particular values.

Every key must correspond to a key in the custom message data that you have set. It is not necessary for all messages to have these keys.

Each value must be one of the following:

A string, equal to "exists" or "!exists"

A 2-element array of [operator, operand] structure. The operand must be either a string or an array of strings (for the oneOf operators). Valid operators are: "==", "!=", "oneOf", and "!oneOf".

Example 1
Only show messages that have no custom category set
{ custom: { category: "!exists" } }
Example 2
Only show messages that have the custom category "shoes"
{
  custom: {
    category: ["==", "shoes"]
  }
}
Example 3
Only show messages that have a 'topic' of either "inquiry" or "reservation"
{
  custom: {
    topic: ["oneOf", ["inquiry", "reservation"]]
  }
}
Example 4
Only show messages about shoes that are marked visible
{
  custom: {
    category: ["==", "shoes"],
    visibility: ["==", "visible"]
  }
}
sender (optional)
: StringFilter

Only show messages if the sender's user ID matches this filter

System messages (which have no sender) will ignore this filter. To exclude system messages, use the type filter instead.

Example
Only show messages sent by users with the user ID "user_123"
{ sender: ["==", "user_123"] }

type StringFilter

A two-element array that forms a predicate about a field.

Used in MessagePredicate and ConversationPredicate. Possible forms:

- ["==", "someValue"]

- ["!=", "someValue"]

- ["oneOf", ["someValue", "someOtherValue"]]

- ["!oneOf", ["someValue", "someOtherValue"]]

type NumberFilter

A two-element array that forms a predicate about a numeric field.

Used in ConversationPredicate. Possible forms:

- [">", someNumber]

- ["<", someNumber]

- [">=", someNumber]

- ["<=", someNumber]

- ["between", [lowerBound, upperBound]]

- ["!between", [lowerBound, upperBound]]

The lower bound is inclusive, the upper bound is exclusive.

type CustomFilter

A string or a two-element array that forms a predicate about a string field in a custom field.

Used in MessagePredicate and ConversationPredicate. Allows all forms in StringFilter plus the following:

- "exists"

- "!exists"

type CompoundFilter

Combines multiple simple filters into one, matching whenever any of the simple filters match (OR).

The first element must be the string "any", and the second element a list of simple filter objects.

See SimpleConversationFilter for all available options when filtering conversations.

See SimpleMessageFilter for all available options when filtering messages.

Example
Only show conversations with messages, and empty conversations with the subject "cats!"
["any", [
  { hasMessages: true },
  { subject: ["==", "cats!"] }
]]