Messages
introduction
using the cyxth sdk users can send and receiver messages in channels, messages can be simple text messages, multimedia messages with media and file attachments or even custom messages and data.
Sending a message
a user must be subscribed to a channel (temporary channels) or a member of a given channel(persistent channel) to send a messages in the channel. if a user id blocked in channel or instance wide this will fail.
sending a message returns the message delivery status . the status is sent if the cyxth server has received the message, delivered if users in channel receieved the message.
send(
channelId: string,
message: MessageBody,
options: sendOptions
): Promise<DeriveryStatus>
example
let message = {
text: 'hello world'
};
cyxth
.send('hello_channel', message, {})
.then((message) => {
console.log(message.id, message.status);
})
.catch((error) => {
console.error(error.reason);
});
sending media
if file storage is enabled in console, users can send add filelist in media, cyxth will detect the media type from the files given and upload them to cyxth managed file server. this will link messages to files enabling easier management and reuse in a channel
// from user upload
// let fileList =
let message = {
text: 'here are my files',
media: {
files: fileList
}
};
// handle file upload progress
const progressHandler = (progress) => {
console.log(progress);
};
cyxth
.send(message, { progressHandler })
.then((sent) => {
// do sth
})
.catch((err) => {
// handle error
});
you can also use your own media using urls
let message = {
text: 'here are my files',
media: {
images: [{ url: 'https://imag.yourapp.com/nfidfkjdij', name: 'name', thumb: 'thumb' }],
videos: [
{ url: 'https://imag.yourapp.com/nfidfkjdij', name: 'name', thumb: 'thumb' },
{ url: 'https://imag.yourapp.com/nfidfkjdij', name: 'name', thumb: 'thumb' }
]
}
};
send data
similary send any data with message using the same send function, this allows you to add more functionality to messages in your application.
let message = {
data: {
type: 'live-commentary',
time: "90 ' +2",
score: '4:1',
teams: 'MCI vs ARS',
commentary: '...'
}
};
Receive messages
listen for new messages by on
events dispatched when a new message is received.
users automatically receive messages from their persitent channels or when they subscribe to a given temporary channel.
cyxth.on('message', (message) => {
console.log(message.channelId);
});
Delete message
users can delete a message that they send, if soft the message is edited to [deleted message]
and can not be edited further.
deleteMessage(
channelId: string,
messageId: string,
soft: boolean = true
): Promise<boolean>
example
cyxth.deleteMessage('messageId', {
//replace message with [deleted message]
soft: true
});
Edit messages
messages can be edited once they are sent. edited messages are updated for every user with an edited flag active.
editMessage(
channelId: string,
messageId: string,
message: MessageUpdate
): Promise<boolean>
example
cyxth.editMessage('messageId', {
content: 'Message object'
});
get messages
if a channel is persitent and message storage option is enabled. saved messages in given
channels can be retrieved with getMessages()
with this method signature. this method is useful when scrolling through
older maessages not cached on users device. you can use a given message id or a date to start from and a limit default 100.
getMessages(
channelId: string,
start: Date | string,
limit: number = 100
): Promise<Message[]>
message activity
editing and deleting messages send corresponding activity notifications to all users
in the given channels you can listen to activity
events to
do actions on events.
method summary
summary of message method signatures.
// send a message
send(
channelId: string,
message: Message,
options: MessageOptions
): Promise<DeriveryStatus>
// delete message
deleteMessage(
channelId: string,
messageId: string,
soft: boolean = true
): Promise<boolean>
// edit a message
editMessage(
channelId: string,
messageId: string,
message: MessageUpdate
): Promise<boolean>
// get messages
getMessages(
channelId: string,
start: Date | string,
limit: number = 100
): Promise<Message[]>