1.1.0 • Published 4 years ago

@samanthashepherddubit/rncometchat v1.1.0

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

Comet Chat Wrapper for React Native

Wrapper for comet chat SDK to handle all the logic for sending and receiving messages. It also provides a Flatlist container that will render them.

Installation

If using yarn:

yarn add @samanthashepherddubit/rncometchat

If using npm:

npm i @samanthashepherddubit/rncometchat

Usage

First you need to initalise and login to comet chat using this helper function.

import { loginCometChat } from '@samanthashepherddubit/rncometchat';

Example of useage:

  React.useEffect(() => {
    /**
     * Login to chat
     */
    loginCometChat(chatUid, apiKey, appId, region);
  }, []);
ParamDescriptionType
chatUidUser's Comet Chat Idstring
apiKeyComet Chat API keystring
appIdComet Chat app IDstring
regionUser's regionstring

Sending a text message

import { sendTextMessage } from '@samanthashepherddubit/rncometchat';

Example of useage:

const message = "Text message to show"

sendTextMessage(chatID, message, receiverType, metaData);
ParamDescriptionType
chatIDID of chat you want to messagestring
messageThe text messagestring
receiverType"GROUP" or "USER"string
metaDataAny metadata you want to attach to the messageobject

Sending a picture message

import { sendMediaMessage } from '@samanthashepherddubit/rncometchat';

Example of useage:

const file = {
  name: "Camera_001.jpeg",
  type: "image/jpeg",
  uri: "file:///Users/flower.jpg"
}

sendMediaMessage(chatID, file, receiverType, metaData);
ParamDescriptionType
chatIDID of chat you want to messagestring
fileThe text message{name: string, type: string, uri: string }
receiverType"GROUP" or "USER"string
metaDataAny metadata you want to attach to the messageobject

Sending a custom message

import { sendCustomMessage } from '@samanthashepherddubit/rncometchat';

Example of useage:

const customType = CustomMessageTypes.POLL;
const customData = { question: "Question", answers: ["Answer 1", "Answer 2"] };

sendCustomMessage(chatId, receiverType, customType, customData, metadata);
ParamDescriptionType
chatIDID of chat you want to messagestring
messageThe text messagestring
receiverType"GROUP" or "USER"string
customTypeType to add to message (CustomMessageTypes has special behaviour)string
customDataData to make up the custom messageobject
metaDataAny metadata you want to attach to the messageobject

Fetching messages

import { fetchMessages } from '@samanthashepherddubit/rncometchat';

Example of useage:

const messages = await fetchMessages(chatId, receiverType, numberOfMessages);
ParamDescriptionType
chatIdID of chat you want messages fromnumber
receiverType"GROUP" or "USER"string
numberOfMessagesNumber of messages to fetch (max 100)number

Clearing messages

import { clearMessages } from '@samanthashepherddubit/rncometchat';

Deleting own message

import { deleteOwnMessage } from '@samanthashepherddubit/rncometchat';

Example of useage:

deleteOwnMessage(messageId);
ParamDescriptionType
messageIdID of message you want to deletenumber

Displaying the messages

import { Chat } from '@samanthashepherddubit/rncometchat';

Example of useage:

<Chat
  renderTextMessage={(message: CometChat.TextMessage) => (
    <ChatTextMessage message={message} />
  )}
  renderImageMessage={(message: CometChat.MediaMessage) => (
    <ChatImageMessage message={message} />
  )}
  renderPollMessage={(message: CometChat.CustomMessage) => (
    <ChatImageMessage message={message} />
  )}
  renderErrorMessage={(error: string) => (
    <ChatImageMessage message={message} />
  )}
  messageConfig={[MessageConfig.TEXT, MessageConfig.FILE]}
  chatID={groupID}
  receiverType={RECEIVER_TYPE}
  messageFetchSize={30}
/>
ParamDescriptionTypeRequired?
messageConfigList of message types you want to send and listen forMessageConfig (see below)Yes
chatIDThe ID of the chat you want to displaystringYes
receiverType"GROUP" or "USER"stringYes
messageFetchSizeNumber of messages to fetch on mount of componentnumberNo (default is 20)
renderTextMessageRenderer for text messages(message: CometChat.TextMessage) => ReactElementNo (default is null)
renderImageMessageRenderer for picture messages(message: CometChat.MediaMessage) => ReactElementNo (default is null)
renderPollMessageRenderer for poll messages(message: CometChat.CustomMessage) => ReactElementNo (default is null)
renderErrorMessageRenderer for any error messages at the bottom of chat(error: string) => ReactElementNo (default is null)

MessageConfig

import { MessageConfig } from '@samanthashepherddubit/rncometchat';

List of message types that will be added to the message listeners | Key | Description |-------------|------------------- | TEXT | For text messages | FILE | For image messages | CUSTOM | For custom messages

CustomMessageTypes

import { CustomMessageTypes } from '@samanthashepherddubit/rncometchat';

List of message types to use in custom messages to do certain behaviour

KeyDescription
POLLUses renderPollMessage to render the message
POLL_RESPONSEUsed to count answers in poll results