Skip to main content

Send a message

Send text, media, and custom messages through the Nexconn Chat SDK.

Prerequisites

  • The SDK is initialized and connected. See Quick start.
  • The user has an active connection (NCEngine.connect returned code === 0).

Send a text message

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

const channel = new DirectChannel('<target-user-id>');

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

Message types

The SDK provides several built-in message types via the MessageType enum:

Enum valueType stringDescription
MessageType.TEXTRC:TxtMsgPlain text content
MessageType.IMAGERC:ImgMsgImage file
MessageType.HD_VOICERC:HQVCMsgHigh-quality voice recording
MessageType.FILERC:FileMsgGeneric file attachment
MessageType.GIFRC:GIFMsgGIF image
MessageType.LOCATIONRC:LBSMsgGeographic coordinates
MessageType.REFERENCERC:ReferenceMsgReply to a specific message
MessageType.COMBINERC:CombineMsgForwarded message set

Send a media message

Media messages (images, files, voice) are uploaded to the file server first, then sent as a message containing the remote URL. Use the Helper factory methods to create the parameter object for each media type.

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

const channel = new DirectChannel('<target-user-id>');

// Use the factory method to build SendImageMessageParams
const result = await Helper.createSendImageMessageParams(file); // file: File object from <input type="file">
if (!result.isOk) {
console.log('Failed to prepare image params. Code:', result.code);
return;
}

const params = result.data;
params.onUploadProgress = (progress, msg) => {
console.log('Upload progress:', progress); // 0–100
};
params.onUploadComplete = (remoteUrl, msg) => {
console.log('Upload complete:', remoteUrl);
};
params.onUploadError = (code, msg) => {
console.log('Upload failed. Code:', code);
};

const { code, data } = await channel.sendMediaMessage(params);
if (code === 0) {
console.log('Media message sent:', data);
}

Other media types have corresponding factory methods:

Message typeFactory method
ImageHelper.createSendImageMessageParams(file)
FileHelper.createSendFileMessageParams(file)
GIFHelper.createSendGIFMessageParams(file)
HD voiceHelper.createSendHDVoiceMessageParams(file)

Send to different channel types

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

// Send to a group channel
const groupChannel = new GroupChannel('<group-id>');
await groupChannel.sendMessage(new SendTextMessageParams({ text: 'Hi group!' }));

// Send to an open channel
const openChannel = new OpenChannel('<open-channel-id>');
await openChannel.sendMessage(new SendTextMessageParams({ text: 'Hi open channel!' }));

// Send to a community subchannel
const subChannel = new CommunitySubChannel('<community-id>', '<subchannel-id>');
await subChannel.sendMessage(new SendTextMessageParams({ text: 'Hi community!' }));

Rate limits

The client SDK enforces a rate limit of 5 messages per second. Messages that exceed the limit are rejected.

Message properties

Each sent message returns a Message<T> object with the following key properties:

PropertyTypeDescription
messageIdstringUnique message ID assigned by the server
sentTimenumberServer-assigned timestamp
messageTypestringThe message type identifier
senderUserIdstringThe sender's user ID
tip

Use sentTime (server time) rather than local time for message ordering to ensure consistency across devices.