Skip to main content

Send and receive messages

Send and receive messages in community subchannels.

Send a message

Messages in community channels are sent to a specific subchannel. Use CommunitySubChannel with the community ID and subchannel ID.

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

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

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

Send a media message

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

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

const result = await Helper.createSendImageMessageParams(file); // file: File object
if (result.isOk) {
const params = result.data;
params.onUploadProgress = (progress) => {
console.log('Upload progress:', progress);
};
const { code, data } = await channel.sendMediaMessage(params);
if (code === 0) {
console.log('Media message sent:', data);
}
}

Receive messages

All incoming messages (including community channel messages) arrive through the global message listener:

TypeScript
import { NCEngine, MessageHandler, Message } from '@nexconn/chat';

NCEngine.addMessageHandler('community-handler', new MessageHandler({
onMessageReceived({ messages }): void {
messages.forEach((msg) => {
console.log('Received:', msg);
});
},
}));
tip

Filter messages by channelType to distinguish community channel messages from other channel types.