@jnk/uc-client
v0.1.23
Published
## Getting started 1. Go to ulti-chat.com, create an account and create your first stack for free. 2. Install client using yarn: yarn add @jnk/uc-client` 3. Save the ulti-chat public key in your project
Downloads
8
Readme
UltiChat Client
Getting started
- Go to ulti-chat.com, create an account and create your first stack for free.
- Install client using yarn: yarn add @jnk/uc-client`
- Save the ulti-chat public key in your project
Usage
Initialize Client
- Import the client
import { WSClient } from '@jnk/uc-client'
- In order to create a user-token you need to create a client with the root-token and run the function
createUser()
. - Create a new client:
const client = new WSClient(userToken, publicKey)
- Open the connection:
await client.open()
Create User
In or to create a user using the UltiChat API you need to initialize the client with a root-token. Then use the createUser()
function:
const response: User = await client.createUser();
type User = {
userToken: string;
userId: string;
};
Create Channel
Channels can be created with root- and user-tokens using the createChannel()
function:
client.createChannel(userIds);
userIds
are the IDs of users which will be added to the newly created channel.
In case you use the client as a 'user' the user's ID is automatically added to the newly created channel - you don't have to put it in the array of userIds but if you do it's fine as well.
createChannel
does not has a return value - you need to set the channelListEventHandler
to get notified if you have been added to a new channel.
Get Channel-List
Retrieve the channels the current user is part of / has been added to:
const channels: ChannelListChannel[] = await client.getChannelList();
type ChannelListChannel = {
id: string;
lastMessage: {
text: string;
timestamp: string;
} | null;
};
Send Message
Providing a channelId and a message you can send messages in specifc chats. For now only plain text messages are possible:
client.sendMessage(channelId, message);
Hint for testing purpose: If you send a message in a channel the client assumes that you previously have fetched the channel-list for the current user (see Get Channel-List) as well as the messages for the specific channel (see Get Channel Messages) .
Get Channel Messages
Retrieve all messages for a specific channel:
const messages: Message[] = await client.getChannelMessages(channelId);
type Message = {
text: string;
timestamp: string;
userId: string;
};
ChannelListEventHandler
You want to set this event handler to get an updated channelList everytime an one of 3 events occurs:
- Current user has been added to a channel
- Current user has been removed from a channel
- A new message has been sent in a channel the user is part of
The method
setChannelListEventHandler
takes a callback as an argument and in case of one of the above event it is called with 2 arguments
eventType
(added_to_channel || removed_from_channel || new_message)channelList
(complete updated channelList)
client.setChannelListEventHandler((eventType, channelList) => {
...
})
ChannelEventHandler
You want to set a channel event handler if a chat window is currently showed in your application. The method setChannelEventHandler
takes a callback as an argument. In case of a new_message in the active channel the callback is called with 3 arguments:
activeChannelId
newMessageChannelId
newMessageContent
client.setChannelEventHandler((activeChannelId, newMessageChannelId, newMesssageContent) => {
...
})
Active Channel
In order to gain full flexibility in the ChannelEventHandler
you want to set the active channel if your a chat is currently showed in your application:
client.setActiveChannel(channelId)
Mock test data
- Use tests (e.g. createChannel or sendMessage) to create test data
- Make sure to use it.only to only run the one test you want to run