simple-chat-client
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

Simple-Chat-Client

Simple-Chat-Client is an entity that connects to an Simple-Chat server.

simple-chat-client package includes set of features to connect and authenticate securely and reliably.

It supports Node.js, browsers and React Native. See below for differences. In connection WebSockets are used. Library is using Socket.Io.

Installation

Use npm or yarn to install the package

npm i --save simple-chat-client
yarn add simple-chat-client

Example usage

import SimpleChatClient from 'simple-chat-client';

const chatClient = new SimpleChatClient({
        host: 'http://localhost',
        port: 3333,
        userId: 'user_id',
        accessToken: 'access_token',
      });

chatClient.start();

chatClient.on("error", (err) => {
  console.log(err.message);
});

chatClient.on('message', (chatMessage: Message, ack: () => void) => {
    ack(); //Need to acknowledge that message received
    ...
    });
  });

const message = {
  to: 'chat_id',
  body: {
    text: 'test',
  },
  timestamp: 123123123123,
};

chatClient?.sendMessage(message)

Params

Following parameters are exposed in Object instance:

status: ConnectionStatus

enum ConnectionStatus {
  CONNECTED = 'connected',
  DISCONNECTED = 'disconnected',
}

API Reference

The following methods are exported by the simple-chat-client module:


start(): void

Create a connection with the server. This should be called on a created object instance. This could be called also before message listeners added.


disconnect(): void

Disconnect from the server.


sendMessage: (message: Message) => Promise<string>

Send messages to chat users. Possible messages are text messages and typing. Users can send typing events without message body or with the message body.

Returns:

message-id saved on the server.

interface Message {
  id?: string;
  to: string;
  timestamp: number;
  body?: {
    [key: string]: any;
  };
  typing?: boolean;
  from?: string;
}

loadArchive: (chatId: string, limit: number, after?: string | null) => Promise<Message[]>

Load chat message archive from a server. To load the latest messages, after should be empty. If it is needed to load messages after a specific message, after should be provided.

Note: if after is not provided last messages will be provided.

Params:

  • chatId (string) -- chat id from which to fetch messages
  • limit (number) -- how many messages to fetch
  • after (string) (optional) -- chat id from which to fetch messages.

Returns:

Message list.


joinChat: (chatId: string, temp?: boolean) => Promise<boolean>

Join chat permanently or temporarily. Temporary means that on disconnect chat join will be removed.

Note: if temp provided as true then the user will be joined temporary, and on disconnection will be automatically removed from chat.

Note: if temp provided as true then system should manually leave chat when the user navigates out of the chat.

Params:

  • chatId (string) -- chat id to which join
  • temp (boolean) (optional)-- flag to join chat temporary

Returns:

boolean if successfully joined or not.


leaveChat: (chatId: string) => Promise<boolean>

Leave chat. When user left chat he will not receive any notifications and messages

Params:

  • chatId (string) -- chat id which to leave

Returns:

boolean if successfully left or not.


createChat: (type: ChatTypes, users: string[]) => Promise<string>

Create chat which will be used in communication. Possible types of the chat are Multi-User chat, and Single User chat For single-user chat it is needed to provide a list of two users who will be participants in the chat.

Params:

  • type (ChatTypes) -- type of the chat.
  • users (string[]) (optional) -- user list for SUC chat. Should contain only two user ids.
enum ChatTypes {
  SUC = '@suc',
  MUC = '@muc',
}

Returns:

string id of the newly created chat.


Available events

Below events are available to listen:

Event connected

chatClient.on('connected', () => {
  console.debug();
});

Event disconnected

chatClient.on('disconnected', () => {
  console.debug();
});

Event error

chatClient.on('error', (error: Error) => {
  console.log(error.message);
});

Event reconnect

chatClient.on('reconnect', (attempt: number) => {
  console.log(attempt);
});

Event reconnect_attempt

chatClient.on('reconnect_attempt', (attempt: number) => {
  console.debug();
});

Event reconnect_error

chatClient.on('reconnect_error', (error: Error) => {
  console.log(error.message);
});

Event reconnect_failed

chatClient.on('reconnect_failed', () => {
  console.log(error.message);
});

Event message

chatClient.on("message", (chatMessage: Message, ack: () => void) => {
    ack(); //Need to acknowledge that message received
    ...
    });
  });

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Package Sidebar

Install

npm i simple-chat-client

Weekly Downloads

0

Version

1.1.0

License

MIT

Unpacked Size

39.8 kB

Total Files

13

Last publish

Collaborators

  • davx1992