1.1.0 • Published 4 years ago
@samanthashepherddubit/rncometchat v1.1.0
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);
}, []);
Param | Description | Type |
---|---|---|
chatUid | User's Comet Chat Id | string |
apiKey | Comet Chat API key | string |
appId | Comet Chat app ID | string |
region | User's region | string |
Sending a text message
import { sendTextMessage } from '@samanthashepherddubit/rncometchat';
Example of useage:
const message = "Text message to show"
sendTextMessage(chatID, message, receiverType, metaData);
Param | Description | Type |
---|---|---|
chatID | ID of chat you want to message | string |
message | The text message | string |
receiverType | "GROUP" or "USER" | string |
metaData | Any metadata you want to attach to the message | object |
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);
Param | Description | Type |
---|---|---|
chatID | ID of chat you want to message | string |
file | The text message | {name: string, type: string, uri: string } |
receiverType | "GROUP" or "USER" | string |
metaData | Any metadata you want to attach to the message | object |
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);
Param | Description | Type |
---|---|---|
chatID | ID of chat you want to message | string |
message | The text message | string |
receiverType | "GROUP" or "USER" | string |
customType | Type to add to message (CustomMessageTypes has special behaviour) | string |
customData | Data to make up the custom message | object |
metaData | Any metadata you want to attach to the message | object |
Fetching messages
import { fetchMessages } from '@samanthashepherddubit/rncometchat';
Example of useage:
const messages = await fetchMessages(chatId, receiverType, numberOfMessages);
Param | Description | Type |
---|---|---|
chatId | ID of chat you want messages from | number |
receiverType | "GROUP" or "USER" | string |
numberOfMessages | Number 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);
Param | Description | Type |
---|---|---|
messageId | ID of message you want to delete | number |
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}
/>
Param | Description | Type | Required? |
---|---|---|---|
messageConfig | List of message types you want to send and listen for | MessageConfig (see below) | Yes |
chatID | The ID of the chat you want to display | string | Yes |
receiverType | "GROUP" or "USER" | string | Yes |
messageFetchSize | Number of messages to fetch on mount of component | number | No (default is 20) |
renderTextMessage | Renderer for text messages | (message: CometChat.TextMessage) => ReactElement | No (default is null) |
renderImageMessage | Renderer for picture messages | (message: CometChat.MediaMessage) => ReactElement | No (default is null) |
renderPollMessage | Renderer for poll messages | (message: CometChat.CustomMessage) => ReactElement | No (default is null) |
renderErrorMessage | Renderer for any error messages at the bottom of chat | (error: string) => ReactElement | No (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
Key | Description |
---|---|
POLL | Uses renderPollMessage to render the message |
POLL_RESPONSE | Used to count answers in poll results |