0.1.23 • Published 5 years ago

@jnk/uc-client v0.1.23

Weekly downloads
-
License
ISC
Repository
-
Last release
5 years ago

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
0.1.23

5 years ago

0.1.21

5 years ago

0.1.20

5 years ago

0.1.19

5 years ago

0.1.18

5 years ago

0.1.17

5 years ago

0.1.15

5 years ago

0.1.14

5 years ago

0.1.13

5 years ago

0.1.12

5 years ago

0.1.11

5 years ago

0.1.10

5 years ago

0.1.9

5 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago