Skip to main content

Quick start: Community channel

This tutorial walks you through integrating the Nexconn Chat SDK and experiencing basic community channel messaging capabilities.

Prerequisites

Register a developer account and obtain your App Key from the Console.

caution

The App Secret is used for generating Server API signatures only. Keep it confidential.

Import the SDK

tip

The Chat SDK provides full TypeScript support. We recommend using TypeScript for better code safety and maintainability.

NPM (recommended)

  1. Install the packages:

    bash
    npm install @nexconn/engine@latest @nexconn/chat@latest -S
  2. Import the modules:

    TypeScript
    import {
    NCEngine,
    CommunityChannel,
    CommunitySubChannel,
    MessageType,
    TextMessageContent,
    Message,
    ConnectionStatusHandler,
    MessageHandler,
    ChannelHandler,
    } from '@nexconn/chat';

Initialize

Call the initialization method exactly once during the app lifecycle:

TypeScript
NCEngine.initialize({ appKey: '<Your-App-Key>' });

Set up listeners

After initialization, register event listeners before connecting.

TypeScript
// Connection status listener
NCEngine.addConnectionStatusHandler('ug-handler', new ConnectionStatusHandler({
onConnectionStatusChanged({ status, code }): void {
console.log('Connection status:', status, 'code:', code);
},
}));

// Message listener
NCEngine.addMessageHandler('ug-handler', new MessageHandler({
onMessageReceived({ messages }): void {
console.log('Messages received:', messages);
},
}));

// Community channel status listener (includes sync completion notification)
NCEngine.addChannelHandler('ug-handler', new ChannelHandler({
onCommunityChannelsSyncCompleted(): void {
console.log('Community channel list sync complete');
},
}));

Connect

Obtain an access token from the Server API, then connect:

TypeScript
NCEngine.connect({ token: '<Your-Token>' }).then((res) => {
if (res.code === 0) {
console.log('Connected. User ID:', res.data?.userId);
} else {
console.warn('Connection failed. Code:', res.code);
}
});

Get the total unread count

info

Community channel features on Web require Community Channel Cloud Storage to be enabled. Call this API only after a successful connection.

TypeScript
const { code, data: unreadCount } = await CommunityChannel.getTotalUnreadCount();
if (code === 0) {
console.log('Community channels total unread:', unreadCount);
}

Send a message

Send a text message to a community subchannel:

TypeScript
import { CommunitySubChannel, SendTextMessageParams } from '@nexconn/chat';

const channel = new CommunitySubChannel('<communityId>', '<subChannelId>');

const params = new SendTextMessageParams({ text: 'Hello, community!' });
const { code, data } = await channel.sendMessage(params);
if (code === 0) {
console.log('Message sent:', data);
} else {
console.log('Send failed. Code:', code);
}

Receive messages

Incoming messages are delivered through the message listener registered in the Set up listeners section.

Get remote messages

TypeScript
import { BaseChannel } from '@nexconn/chat';

const channel = new CommunitySubChannel('<communityId>', '<subChannelId>');

const query = BaseChannel.createMessagesQuery({
channelIdentifier: channel.identifier,
startTime: Date.now(),
pageSize: 20,
isAscending: false,
});
const { code, data: page } = await query.loadNextPage();
if (code === 0) {
console.log('Messages:', page.data);
} else {
console.log('Failed to get messages. Code:', code);
}

Disconnect

warning

After disconnecting, the user cannot send or receive messages.

TypeScript
await NCEngine.disconnect();
console.log('Disconnected');

Next steps

You have completed the basic community channel integration. See the following sections for detailed guides on subchannel management, user groups, message history, and more.