npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rn-webim-chat-most

v2.8.6

Published

Webim chat wrapper for React-Native. Supports Android and iOS. Fixed issues for native platforms build that are present in the official package.

Downloads

7

Readme

rn-webim-chat-most

Implementation of webim sdk for react-native

Inspired by volga-volga/react-native-webim Inspired by mav10/rn-webim-chat


Platforms:

React Native

Android

iOS

Installation

  • Requires React Native version 0.60.0, or later.
  • Supports iOS 10.0, or later.

Via NPM

npm install rn-webim-chat-most

Via Yarn

yarn add rn-webim-chat-most

:iphone:iOS (Extra steps)

  • add # WebimMobileSDK to Podfile with specific version (Wrapper was written for v3.42.0)
  • pod install

see example Podfile

Since the official WebimClientLibrary is written is Swift, you need to have Swift enabled in your iOS project. If you already have any .swift files, you are good to go. Otherwise, create a new empty Swift source file in Xcode, and allow it to create the neccessary bridging header when prompted.

Example

In example folder there is simple workflow how to:

  • Start and destroy session
  • Resume and Pause session
  • Get and Send messages
  • Rate operator
  • Handle errors

How it looks like you can see here It is achieved with simple UI (just test common methods)

Also there is another example with chat UI react-native-gifted-chat

Methods

Important: All methods are promise based and can throw exceptions. List of error codes will be provided later as get COMMON for both platform.

Init chat

import { RNWebim } from 'rn-webim-chat-most';

await RNWebim.initSession(builderParams: SessionBuilderParams)

SessionBuilderParams:

  • accountName (required) - name of your account in webim system
  • location (required) - name of location. For example "mobile"
  • accountJSON - encrypted json with user data. See Start chat with user data
  • clearVisitorData - clear visitor data before start chat
  • storeHistoryLocally - cache messages in local store
  • title - title for chat in webim web panel
  • providedAuthorizationToken - user token. Session will not start with wrong token. Read webim documentation
  • pushToken - FCM token is enough - but Apple pushes will come through APN, so you are not able to process them in app by default.
  • appVersion - version of your Application
  • prechat - some additional fields to prechat

Resume session

If you have already initialized a session you should resume it to consume and send messages, get actual information by listeners etc.

NOTE: After that execution operator on web chat will get message that user opens a chat.

import { RNWebim } from 'rn-webim-chat-most';

await RNWebim.resumeSession()

Pause session

If you have already initialized a session you should resume it to consume and send messages, get actual information by listeners etc. After that execution operator on web chat will get message that user opens a chat.

import { RNWebim } from 'rn-webim-chat-most';

await RNWebim.pauseSession()

Init events listeners

import { RNWebim,  WebimEvents} from 'rn-webim-chat-most';

const listener = RNWebim.addNewMessageListener(({ msg }) => {
  // do something
});
// usubscribe
listener.remove();

// or
const listener2 = RNWebim.addListener(WebimEvents.NEW_MESSAGE, ({ msg }) => {
    // do something
});

Supported events (WebimEvents):

  • WebimEvents.NEW_MESSAGE;
  • WebimEvents.REMOVE_MESSAGE;
  • WebimEvents.EDIT_MESSAGE;
  • WebimEvents.CLEAR_DIALOG;
  • WebimEvents.TOKEN_UPDATED;
  • WebimEvents.ERROR;
  • WebimEvents.STATE;
  • WebimEvents.UNREAD_COUNTER;
  • WebimEvents.TYPING;

Get messages

As you called getAllMessages after that you should call nextMessages as reading "all messages" during the same session will get no result (native implementation uses holder and cursor by last loaded message)

const { messages } = await RNWebim.getLastMessages(limit);
// or
const { messages } = await RNWebim.getNextMessages(limit);
// or
const { messages } = await RNWebim.getAllMessages();

Message type

export type WebimMessage = {
  id: string;
  serverSideId: string;
  avatar?: string;
  time: number;
  type: MessageTypeAlias; // 'OPERATOR', 'VISITOR', 'INFO', 'ACTION_REQUEST', 'CONTACTS_REQUEST', 'FILE_FROM_OPERATOR', 'FILE_FROM_VISITOR', 'OPERATOR_BUSY', 'KEYBOARD', 'KEYBOARD_RESPONSE';
  text: string;
  name: string;
  status: 'SENT' | 'SENDING';
  read: boolean;
  canEdit: boolean;
  carReply: boolean;
  isEdited: boolean;
  canReact: boolean;
  canChangeReaction: boolean;
  visitorReaction?: string;
  stickerId?: number;
  quote?: Quote;
  attachment?: WebimAttachment;
  operatorId?: string;
}

Quote type

export type Quote = {
  authorId?: string;
  senderName: string;
  messageId: string;
  messageText: string;
  messageType: MessageTypeAlias;
  state: 'FILLED' | 'NOT_FOUND' | 'PENDING';
  timestamp: Date | number;
  attachment?: WebimAttachment;
};

Included attachment

export interface WebimAttachment {
  contentType: string;
  info: string;
  name: string;
  size: number;
  url: string;
}

Note: method getAllMessages works strange on iOS, and sometimes returns empty array. We recommend to use getLastMessages instead

Send text message

import RNWebim from 'rn-webim-chat-most';

const messageId = await RNWebim.send(message);

Read Messages (mark as read)

You can manually mark all messages as read by calling this method.

import RNWebim from 'rn-webim-chat-most';

await RNWebim.readMessages();

Attach files

Use build in method for file attaching:

In future will add possibility to use external library as react-native-fs and some other picker to import files via them. For now there are such methods

Attach file

var result: AttachFileResult = await RNWebim.tryAttachAndSendFile();

console.log('uri: ', result.uri)
console.log('name: ', result.name)
console.log('mime: ', result.mime)
console.log('extension: ', result.extension)

Send file

import RNWebim from 'rn-webim-chat-most';

try {
  RNWebim.sendFile(uri, name, mime, extension)
  console.log('Result: ', sendingResult.id)
} catch (e) {
  // can throw such errors
  'FILE_SIZE_EXCEEDED', 'FILE_SIZE_TOO_SMALL', 'FILE_TYPE_NOT_ALLOWED', 'MAX_FILES_COUNT_PER_CHAT_EXCEEDED', 'UPLOADED_FILE_NOT_FOUND', 'UNAUTHORIZED',
}

Rate current operator

RNWebim.rateOperator(rate: number)
  • rate (required) - is number from 1 to 5

Get current operator

import RNWebim from 'rn-webim-chat-most';

RNWebim.getCurrentOperator()

it returns such object

export type Operator = {
  id: string;
  name: string;
  avatar?: string;
  title: string;
  info: string;
};

Destroy session

RNWebim.destroySession(clearData);
  • clearData (optional) boolean - If true wil

Start chat with user data

Tl;DR; You have to generate private key in your Webim Account and kinda sign your user fields values. For more details see webim documentation for client identification.

in Example app there is code how to achieve it. Example:

I'd recommend to you use some lightweight library. HMAC-256 is enough. Actually you can use md5 algorithm - but I'd avoid it. There are some other aproches e.g. with JsCrypto or with react-native-crypto . But here you need to hash all your modules. Like here. But the choice it is up to you!

  • install js-sha512
  • write hash-function to sign your fields.
  • use it in your app.
// chat-utils.ts file
import { sha512 } from 'js-sha512';

const getHmac_sha512 = async (str, privateKey) => {
  return sha512.hmac(privateKey, str);
};

/**
 * Returns hash value for authorized user.
 * @param obj - User's json fields.
 * @param privateKey - private key value. By that hash will be generated.
 */
export const getHashForChatSign = async (
  obj: { [key: string]: string },
  privateKey: string
) => {
  const keys = Object.keys(obj).sort();
  const str = keys.map((key) => obj[key]).join('');
  return await getHmac_sha512(str, privateKey);
};
// App.tsx file
...
import { getHashForChatSign } from './chat-utils';
const intSession = useCallback(async () => {
    try {
      setLoad(true);
      const userFields = {
        fields: {
          id:'',
          display_name: '',
          phone: '',
          email:'',
        },
        hash: '',

      };
      userFields.hash = await getHashForChatSign(userFields.fields, privateKey);
      const firebaseToken =
        Platform.OS == 'ios'
          ? await messaging().getAPNSToken()
          : await messaging().getToken();

      const sessionsParams = {
        accountName: chatAccount,
        location: 'Mobile',
        title: '',
        appVersion: DeviceInfo.getVersion(),
        pushToken: firebaseToken,
        clearVisitorData: false,
      };

      if (userAuth) { // if auth user
        sessionsParams.accountJSON = JSON.stringify(userFields);
      }
      await RNWebim.initSession(sessionsParams);
  
      await RNWebim.resumeSession(departmentKey);
      listeners();
      const messageResult = await RNWebim.getNextMessages(15);
      setResult(messageResult.reverse());
      setLoad(false);
    } catch (err) {
      setLoad(false);
      console.log('[Chat][Init] error: ', JSON.stringify(err), '\n', err);
    }
  }, []);

...

Contributing

See the contributing guide guide to learn how to contribute to the repository and the development workflow.

License

Software provided as it is. It will be maintained time-to-time. Currently, I have to use this package in some applications, so I try to keep it on working. If you want to help or improve something see section #Contributing


Made with create-react-native-library