Migrate ccs-chat-notifications-client 0.x to 1.0.0
Upgrade your web app from ccs-chat-notifications-client 0.x to 1.0.0. The 1.0.0 client targets the Chat API that is scoped to a Context. One Chat token now serves every tenant and application the user belongs to, so the realtime client has to be told which one it listens in.
Prerequisites
Section titled “Prerequisites”- A web app on
ccs-chat-notifications-client0.x. - A
getAccessTokenthat returns the user’s Chat token:audischat-api, scopechat:access, issued to your application’s client. See Get the Chat token in your app. - Your
tenantIdandapplicationId.
What changed
Section titled “What changed”| 0.x | 1.0.0 |
|---|---|
new ChatNotificationsClient({ chatApiUrl, getAccessToken }) |
Also requires tenantId and applicationId. |
GET /notifications/realtime-token?channelId=<id> |
GET /tenants/{tenant_id}/applications/{application_id}/notifications/realtime-token?conversation_id=<id> |
Realtime-token response streamUrl |
stream_url (snake_case, like every Chat API field) |
subscribeToChannel(id) and unsubscribeFromChannel(id) |
subscribeToConversation(id) and unsubscribeFromConversation(id), also in the Angular and React wrappers |
camelCase event payloads (messageId, senderId, content, productId, …) |
The API’s snake_case payloads. They always carry tenant_id and application_id, plus conversation_id. message.created wraps the full message as event.message. |
Events message.created, typing, presence.updated |
Adds message.updated, message.deleted, reaction.added, reaction.removed, member.added, member.removed, conversation.created, conversation.updated, conversation.deleted and conversation.access_revoked. |
new ChatPushClient({ chatApiUrl, getAccessToken }) |
Also requires tenantId. Devices live at /tenants/{tenant_id}/notifications/push/devices. |
VAPID key response publicKey |
public_key |
The client handles the route changes itself. Only an app that calls the realtime-token or push routes directly has to change them.
-
Upgrade the package. Install
@central-code-station/ccs-chat-notifications-client1.0.0 or later. -
Pass the context. Add
tenantIdandapplicationIdto everyChatNotificationsClient, andtenantIdto everyChatPushClient. In the Angular and React wrappers, put them in the options object you already pass. -
Rename the subscribe calls. Use
subscribeToConversation(id)andunsubscribeFromConversation(id). -
Update your event handlers. Read the snake_case fields. For
message.created, destructuremessageand readmessage.body,message.conversation_idandmessage.sender. They are the sameChatMessageshape as inccs-chat-user-client. -
Recreate the client when the context changes. Switching tenant or application means disconnecting and creating a new client for the new context. See Multi-tenant apps.
// 0.xnotifications.on('message.created', (e) => show(e.messageId, e.content));// 1.0.0notifications.on('message.created', ({ message }) => show(message.message_id, message.body));The same wiring on 1.0.0:
import { ChatNotificationsClient } from '@central-code-station/ccs-chat-notifications-client';import { ChatPushClient } from '@central-code-station/ccs-chat-notifications-client/push';
declare function getChatToken(): Promise<string>;
const chatApiUrl = 'https://chat.centralcodestation.com/api/v1';const tenantId = 'fitness';const applicationId = 'fitness-app';
// Realtime: tenantId and applicationId are now required.export const live = new ChatNotificationsClient({ chatApiUrl, tenantId, applicationId, getAccessToken: getChatToken });
live.on('message.created', ({ message }) => { // snake_case payloads; message.created wraps the full message. console.log(message.conversation_id, message.sender?.display_name, message.body);});live.connect();
// subscribeToChannel became subscribeToConversation.live.subscribeToConversation('6b1f4c7e-3c52-4a0a-9a39-7d2e2f1f8a10');
// Web push: tenantId is now required.export const push = new ChatPushClient({ chatApiUrl, tenantId, getAccessToken: getChatToken });export const registered = () => push.register();Verify
Section titled “Verify”Connect the client and send a message to the user from your backend. The message.created handler fires with a message whose tenant_id and application_id match your context, and connectionStateChange reports connected.
Troubleshooting
Section titled “Troubleshooting”| Symptom | Cause and fix |
|---|---|
TypeScript: tenantId or applicationId is missing |
They are required in 1.0.0. |
Handlers receive undefined for messageId or content |
Payloads are snake_case now. Read message.message_id and message.body. |
subscribeToChannel is not a function |
Renamed to subscribeToConversation. |
| The stream never connects | The token is not a Chat token for your application’s context. See Troubleshooting. |