One conversation often holds more than one kind of message, while a view in your app needs only some of them: the replies in one thread, the announcements in a team channel, or the user messages without the system messages around them.
TalkJS message filters let a chat UI or a data subscription show only the messages in a conversation that match a filter. In the Components SDK 0.3.16, the Chatbox takes a messageFilter prop. In the JavaScript Data API (@talkjs/core 1.13.0), subscribeMessages takes a filter option. Both use the same MessageFilter format and stay up to date in real time.
How message filters work in the Components SDK Chatbox
In the Components SDK, the Chatbox shows only the messages that match its messageFilter prop. The following example shows only messages whose custom topic field is inquiry or reservation:
import { Chatbox } from "@talkjs/react-components";
<Chatbox
appId="<APP_ID>"
userId="<USER_ID>"
conversationId="<CONVERSATION_ID>"
messageFilter={{ custom: { topic: ["oneOf", ["inquiry", "reservation"]] } }}
/>;
When your app passes a new value for messageFilter, the Chatbox subscribes again with the new filter, so one Chatbox can switch between views. The <t-chatbox> web component takes the same messageFilter property. In Components SDK 0.3.16, messageFilter is available on the Chatbox only, not on the Inbox.
A message filter can match a message's type (user message or system message), its sender by user ID, its createdAt time, and any custom field you set on messages. To match messages that meet any one of several filters, combine them with ["any", [...]]. System messages have no sender, so a sender filter doesn't apply to them. To leave system messages out, filter on type.
Filter message subscriptions with the JavaScript Data API
The JavaScript Data API is for building your own chat UI or working with chat data in the browser. From @talkjs/core 1.13.0, ConversationRef.subscribeMessages accepts a filter, and the subscription then returns only the messages that match it. The following example keeps a banner with the latest announcement in a conversation:
import { getTalkSession } from "@talkjs/core";
const session = getTalkSession({ appId: "<APP_ID>", userId: "<USER_ID>" });
const conversation = session.conversation("<CONVERSATION_ID>");
const sub = conversation.subscribeMessages(
{ filter: { custom: { category: ["==", "announcement"] } } },
(messages) => {
// messages is newest first; showBanner is your own UI code
showBanner(messages?.[0]?.plaintext);
},
);
A filtered subscription starts with the 30 newest matching messages, and loadMore fetches older ones. Your callback runs again when a matching message is sent, edited, or deleted, and also when an edit moves a message into or out of the filter. Apps that use the Data API through the Classic JavaScript SDK get the same filter option in talkjs 0.47.0.
What you can build with message filters
Message filters can match custom fields that you set on messages, so one conversation can back several views in your app:
- A thread view beside the main chat. The Conversation threads guide tags replies with a custom
threadId. The main Chatbox hides tagged messages with"!exists", and a second Chatbox shows the replies to one message. - Topic or announcement views. Tag messages with a custom field such as
categoryas they're sent, with the Chatbox'sbeforeSendMessagecallback. Then show one category in its own Chatbox or banner. - A chat without system messages. The filter
{ type: ["==", "UserMessage"] }leaves system messages out of the message list. - Recent history only. A
createdAtfilter such as[">", timestamp]shows only messages sent after a given time.
Message filters FAQ
Can I filter messages in the React Native or Flutter SDK?
Yes. The React Native SDK, the Flutter SDK, and the Classic JavaScript and React SDKs already had message filters, each with its own filter format. The Message filters overview links the option for each SDK.
Can a message filter match on origin?
No. The MessageFilter in the Components SDK and the JavaScript Data API matches on type, sender, createdAt, and custom fields. To filter on something else, set it as a custom field on the message.
Get started with message filters
For the Chatbox prop, upgrade @talkjs/react-components or @talkjs/web-components to 0.3.16 or later. For filtered subscriptions, upgrade @talkjs/core to 1.13.0 or later. Every field and operator is in the messageFilter prop reference and the MessageFilter reference.