Use your block list
Use your app's block list to control who users can message.
A user's block list should work across your platform, including chat. This guide shows how to use your app's existing block list to decide who users can message in TalkJS.
Your application decides how users reach a chat. Apply the block list where they find someone to contact, whether that's booking a service or choosing a person from a list:
- Booking marketplaces: If a seller has blocked a buyer, leave that seller out of the buyer's catalogue and prevent the buyer from booking them. A booking then can't create a chat between those users.
- People lists: Leave out people who have blocked the current user when building the list of people they can message.
- Profile pages: Hide or disable the Message button when contact is blocked.
Your application can filter these lists before returning them to the frontend, or include a canMessage flag with each person. That flag tells the frontend whether the signed-in user can contact that person, without needing to load a separate block list in the browser.
For example, check canMessage in your start-chat function as well as using it to decide whether to show the Message button. createAndOpenConversation represents your existing code for starting a chat:
async function startChat(person) {
if (!person.canMessage) {
showError("You can't message this person.");
return;
}
await createAndOpenConversation(person.id);
}Your frontend can continue creating TalkJS conversations as it does now.
A technically minded user could skip your UI and ask TalkJS directly to create a chat with someone who blocked them. For most customers, this isn't a concern. If your platform needs stricter enforcement, create conversations on your backend instead.
Rather than creating the conversation directly from the frontend, send the ID of the person to contact to an endpoint on your backend. The endpoint should:
- Read the signed-in user's ID from your existing session.
- Check the block list and return HTTP
403if contact is blocked. - Generate the conversation ID in the same way your frontend does today, create the conversation through the TalkJS REST API, and return
{ "conversationId": "..." }.
The server sends this request to TalkJS, using your usual REST API authentication:
PUT https://api.talkjs.com/v1/<APP_ID>/conversations/<CONVERSATION_ID>
Authorization: Bearer <ADMIN_JWT>
Content-Type: application/json
{
"participants": ["<CURRENT_USER_ID>", "<OTHER_USER_ID>"]
}For an endpoint named /api/chats, the frontend code becomes the following. openConversation represents your existing code for displaying a conversation:
async function startChat(person) {
const response = await fetch('/api/chats', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ otherUserId: person.id }),
});
if (response.status === 403) {
showError("You can't message this person.");
return;
}
if (!response.ok) {
showError("Couldn't start the chat. Please try again.");
return;
}
const { conversationId } = await response.json();
openConversation(conversationId);
}TalkJS must reject requests to create conversations from the frontend, so users cannot skip the block list check on your backend.
In the TalkJS dashboard, open Settings → Security settings and disable conversation synchronization. See Browser synchronization.
Updates to conversation details and participants must also go through your backend. Apply the block list check wherever your backend adds participants. Users can still read and send messages in their conversations as before.