chat

@cyxth/chat package adds chat functionalities to your application. With cyxth chat your users can send and receive messages, data and files in channels in real-time. and with @cyxth/extras functionalities like end to end encryption and storage can be added to create full fledged chat applications.

this simple guide takes you through setting up chat and sending messages, data and files between users in channels.

prerequisites

first make sure you have created a cyxth account and created a cyxth instance, and make sure to check out authorization and concepts.

next download @cyxth/core and @cyxth/chat from npm

#  core
npm install @cyxth/core
# chat
npm install @cyxth/chat
# add types 
npm install --save-dev @cyxth/types

This guide gives a high level overview and chat concepts, feel free to look around the chat api reference and chat examples after this guide. for a deeper understanding of various topics.

initiailization

chat is a cyxth core plugin and is instantiated like the other plugins to get started with chat use chat() function from cyxth core module.

import Cyxth from '@cyxth/core';
import Chat from '@cyxth/chat';

const cyxth = new Cyxth("YOUR_APP_URL");
cyxth.register([Chat]);

await cyxth.connect(USER_TOKEN_SMH);
const chat: Chat = cyxth.chat();

remember the user has to be connected to cyxth or else cyxth.chat() will fail.

channels

chat extends the functionality of cyxth channels. all actions that can be done on a cyxth channel can be done on chat channels. users can create() , join() , manage and configure channels. we go into much more details on channels in the concepts guide. check out channel concepts for more info.

send and receive

the core functionality of cyxth chat is sending and receiving messages in channels. messages sent in channels can be simple text messages, custom JSON or binary messages all depending on your specific use case.

messages are linked together through replies creating nested message trees. with message storage feature enabled, cyxth can store historical messages. file uploads and linking files to messages is also supported with the file storage feature enabled.

end to end encryption, offline storage and other features are provided in the @cyxth/extras packages.

send

using the chat send() method you can send any text message, json data or file to users in a given channel. users will receive the message with the on() message event.

// ..
// simple text message
chat.send("channelId", "hello world");

// data message with reply
chat.send("channelId", {
    data : {
        wallet_qr: "private.png",
        amount: 0.0006,
        productId: "product"
    },
    replyTo: '7890897834984'
})

// ...

any message can have a replyTo and files field. a message with reply to is linked to the parent message and will be deleted when the parent message is deleted. when listing messages replies are nested with the parent message

fi

receive

use the on() function to listen for new messages across all the user’s connected channels.

chat.on("message",(msg) => {
    console.log(`message on ${msg.channelId} from ${msg.sender}`)
})

events

all cyxth channel events are supported for chat, they can be optionally saved as channel activity messages . i.e when a user joins the channel a data message is created at that point, when listing messages if storage is enabled channel events are shown as activities in the message tree

history

if you have enabled message storage for your cyxth application users can retrive older messages from a given date or given message id. this paired with offline support will enable you to create chat applications such as telegram.

getMessages(
 start: Date | string,
 limit: number = 50,
 channels: undefined | string | string[] = undefined,
 direction: 'reverse' | 'forward' = 'forward'
): Promise<{ [key: string]: Message[] }>

examples

// using id and limit, 150 is max at once
chat.channnels
 .getMessages('7085164563653595136', 150)
 .then((messages) => {
  console.log(messages);
 });

config

extras