@jnk/uc-client
TypeScript icon, indicating that this package has built-in type declarations

0.1.23 • Public • Published

UltiChat Client

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

Usage

Initialize Client

  1. Import the client import { WSClient } from '@jnk/uc-client'
  2. In order to create a user-token you need to create a client with the root-token and run the function createUser().
  3. Create a new client: const client = new WSClient(userToken, publicKey)
  4. 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:

  1. Current user has been added to a channel
  2. Current user has been removed from a channel
  3. 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

/@jnk/uc-client/

    Package Sidebar

    Install

    npm i @jnk/uc-client

    Weekly Downloads

    50

    Version

    0.1.23

    License

    ISC

    Unpacked Size

    69.4 kB

    Total Files

    94

    Last publish

    Collaborators

    • dadodo
    • faeh
    • freshmdb
    • jnk
    • yemboo