Welcome to ChatLib with socket.io
This documentation provides an overview of the Chat
, Network
class and its methods.
Table of Contents
Installation
npm install chat-app-workhub --save
or
yarn add chat-app-workhub
We need to install axios socket.io-client before using libs
Methods
Default api response
{
"data": "response api",
"success": true,
}
Error definition
header status
:
- 200: success
- 400: bad request
- 401: Unauthorized
- 404: Not Found
- 500, 501: Server Error
response error
:
{
"error": {
"code": "string",
"message": "string",
"details": "string",
"data": {
"additionalProp1": "string",
"additionalProp2": "string",
"additionalProp3": "string"
},
"validationErrors": [
{
"message": "string",
"members": [
"string"
]
}
]
}
}
Chat
Method | Description |
---|
connect | Create connections |
disconnet | Stop connections |
sendMessage | send message with group |
updateMessage | update message with group |
receiveMessage | Listens for incoming mes |
receiveDeleteMessage | Listens for incoming mes delete |
setMessageSeenStatus | Sets the "message seen" |
pinMessage | |
unpinMessage | |
reactMessage | |
receiveMessageReaction | |
setMessageSeenStatus | |
receiveMessageSeenStatus | |
revokeMessage | |
onRevokeMessage | |
revokeAttachment | |
onRevokeAttachment | |
sendMessage
Listens for incoming chat messages and invokes a callback function when a new message is received.
chat.sendMessage(
{
message: '',
attachments: [
{
name: '',
desciption: '',
id: '',
url: '',
typeAttachment: 1,
sizeByte: 0,
durationSeconds: 0,
revoke: 1,
},
],
forwardFromRoomId: '',
mentionIds: [],
type: 0,
quoteMessageId: '',
forwardMessageId: '',
},
id
);
receiveMessage
Listens for incoming chat messages and invokes a callback function when a new message is received.
chat.receiveMessage((message) => {});
note: Before using it, you have to initialize the data retrieval function.
import {
ChatUser,
Chat,
InitChatSocket,
InitChatMagement,
} from 'chat-app-demo-abc';
const chatUser = InitChatUser(url, token);
or;
const chatUser = new ChatUser(url, token);
const apiMessage = InitMessageReader(url, token);
or;
const apiMessage = new MessageReader(url, token);
const apiManage = new ChatMagement('url_chat_manager', token);
or;
const apiManage = InitChatMagement('url_chat_manager', token);
const chat = new InitChatSocket('url_chat_soket', token);
or;
const chat = ChatSocket('url_chat_socket', token);
- Except for the login function, you need to pass the token for all other functions.
- authUrl, messageReaderUrl, ChatManageUrl: You only need to pass one of the three. Use the cluster's URL you're using.
Auth
Method | Params | Description |
---|
Login | userName:String, passWord: string | get token using to conect chat |
Login
const result = await ChatUser.login(param);
{
"userName": "string",
"passWord": "string",
"pushToken": "string"
}
import jwt_decode from "jwt-decode";
var infoUser = jwt_decode(access_token);
note: if you need get info user ex: useId, name... you have to instal jwt-decode to decode access_token then you have it
register
const result = await ChatUser.register(param);
{
"username": "string",
"email": "string",
"phoneNumber": "string",
"gender": 0,
"avatar": "string",
"dateOfBirth": "2023-10-06T02:43:24.154Z",
"password": "string",
"image": {
"fileId": "string",
"fileName": "string",
"extension": "string",
"fileUrl": "string",
"contentType": "string"
},
"isAdmin": false
}
import jwt_decode from "jwt-decode";
var infoUser = jwt_decode(access_token);
listUserContactAdd
find list user by contact(phone)
const param = ['09xxx', '08xxx'];
const result = await chatUser.listUserContactAdd(param);
searchFriends
find friends
const result = await chatUser.searchFriends(param);
{
"GroupId": "string",
"Filters": "string",
"phoneNumber": "string",
"Sorting": "ASC",
"SkipCount": 0,
"MaxResultCount": 0,
}
userDoAction
User action(add friend, unfriend, block , unblock)
const result = await chatUser.userDoAction(param);
{
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"actionType": 0
}
}
listFriends
const result = await chatUser.listFriends(param);
{
"GroupId": "string",
"Filters": "string",
"phoneNumber": "string",
"Sorting": "ASC",
"SkipCount": 0,
"MaxResultCount": 0,
}
Message management
ListRoomChat
Retrieves a list of chat rooms in which a user has participated.
const param = { userId: '', skip: 0, take: 20 };
const result = await apiManage.ListRoomChat(param);
{
"userId": "string",
"groupName":"",
"tagFilter": null,
"skip": 0,
"take": 0
}
searchRoomChat
Searches for chat rooms based on specified criteria.
const result = await apiManage.searchRoomChat(param);
{
"roomType":"",
"filter": '',
"skip": 0,
"take": 0
}
getTotalUnread
Searches for chat rooms based on specified criteria.
const result = await apiManage.getTotalUnread();
groupParticipants
Searches for chat rooms based on specified criteria.
const result = await apiManage.groupParticipants(groupId, param);
{
"skip": 0,
"take": 0
}
getInfoRoomChat
Searches for chat rooms based on specified criteria.
const result = await apiManage.getInfoRoomChat(groupId);
groupId: '';
createRoomChat
Searches for chat rooms based on specified criteria.
const result = await apiManage.createRoomChat(param);
{
"name": "string",
"roomType": 0,
"typeOfGroup": 0,
"topic": "string",
"description": "string",
"image": "string",
"ownerId": "string",
"participants": [
"string"
],
"tags": [
"string"
],
"sendDefaultMessage": true,
"defaultMessageContent": "string"
}
updateInfoRoom
Searches for chat rooms based on specified criteria.
const result = await apiManage.updateInfoRoom(param);
{
"name": "string",
"topic": "string",
"description": "string",
"image": "string"
}
deleteChatRoom
Searches for chat rooms based on specified criteria.
const result = await apiManage.deleteChatRoom(roomId);
"roomId":''
joinChatGroup
Searches for chat rooms based on specified criteria.
const result = await apiManage.joinChatGroup(groupId, userId);
{
"groupId": "string",
"userId": "string"
}
inviteChatGroup
Searches for chat rooms based on specified criteria.
const result = await apiManage.inviteChatGroup(param);
{
"groupId": "string",
"userId": [
"string"
]
}
inviteChatGroup
Searches for chat rooms based on specified criteria.
const result = await apiManage.removeUserChatGroup(param);
{
"groupId": "string",
"userId": "string"
}
Message reader management
chatHistory
Retrieves the chat history of a group.
const param = { userId: '', skip: 0, take: 20 };
const result = await apiMessage.chatHistory(roomid, param);
{
"before":"",
"after": '',
"isPined":null,
"skip": 0,
"take": 0
}
historyAttachment
Retrieves the history of attachments in a group.
const result = await apiMessage.historyAttachment(roomId, param);
{
"before":"",
"after": '',
"types": [],
"skip": 0,
"take": 0
}
searchMessegeInGroup
Retrieves the history of message in a group.
const result = await apiMessage.searchMessegeInGroup(roomId, param);
{
"textSearch": '',
"skip": 0,
"take": 0
}
getHistoryAroundMessage
Retrieves the history of attachments in a group.
const result = await api.getHistoryAroundMessage(param);
{
"roomId": '',
"messageId": '',
"take": 10
}
getLastMessagePined
Retrieves the history of attachments in a group.
const result = await api.getLastMessagePined(roomid);
Optional
if you don't have server saved files(audio, image, gift, video) you can using this server for that
const formData = new FormData(form);
const result = await ChatUser.upload('url_server_upload', formData);
Usage
Connect and receive
import { ChatSocket } from 'chat-app-demo-abc';
const ChatScreen = () => {
const chat = new ChatSocket('url_socket', token);
useEffect(() => {
chat.connect();
chat.receiveMessage((message) => {
});
return () => {
chat.disconnect();
};
}, []);
const sendMessage = () => {
const param = {
message: 'text test',
attachments: [],
forwardFromRoomId: '',
mentionIds: [],
type: 0,
quoteMessageId: '',
forwardMessageId: '',
};
chat.sendMessage(param, id);
};
};
login
const api = InitChatAuth({
authUrl: 'http://123.30.145.67:14100/',
token: '',
});
const handelLogin = useCallback(async () => {
const result = await api.Login({
passWord: 'paasword',
userName: 'username',
});
setToken(result.access_token);
}, [api]);