chat-app-workhub
v0.2.43
Published
chat and auth socket
Downloads
21
Readme
Welcome to realtimeChat 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", // response all api
"success": true, // status api true: success, false : have error
}
Error definition
header status
:- 200: success
- 400: bad request
- 401: Unauthorized
- 404: Not Found
- 500, 501: Server Error
response error
:
{
"error": {
"code": "string", // code error
"message": "string", // message
"details": "string", // detail error
"data": {
"additionalProp1": "string", // It cannot determined
"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: '', // message sent
attachments: [
{
name: '', // name file
desciption: '',
id: '', // id attachments (send id = null or '')
url: '', // url file
typeAttachment: 1, // - video = 1, // - audio = 2// - image = 3// - file = 4 // - other = 5
sizeByte: 0,
durationSeconds: 0,
revoke: 1, // 1 revoke for sender, 2 revoke for all group
},
],
forwardFromRoomId: '',
mentionIds: [], // list userId mention
type: 0, //type message list in here:
// - text = 0
// - audio = 1
// - file = 2
// - sticker = 3
// - gallery = 4
// - video = 5
quoteMessageId: '', // message quote id
forwardMessageId: '', // message forward id
},
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';
// auth config
const chatUser = InitChatUser(url, token); // url auth (http://123.30.145.67:14100/) . token : your access token (string)
or;
const chatUser = new ChatUser(url, token);
// auth message reader
const apiMessage = InitMessageReader(url, token); // url auth (http://123.30.145.67:14103/) . token : your access token (string)
or;
const apiMessage = new MessageReader(url, token);
// chat management
const apiManage = new ChatMagement('url_chat_manager', token); // url http://123.30.145.67:14101/
or;
const apiManage = InitChatMagement('url_chat_manager', token);
// chat management
const chat = new InitChatSocket('url_chat_soket', token); // url http://123.30.145.67:14110/
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);
// param request
{
"userName": "string", //email account login
"passWord": "string", // password
"pushToken": "string"// pushtoken is device token in firebase
}
// this way to get info user
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);
// param request
{
"username": "string",
"email": "string",
"phoneNumber": "string",
"gender": 0,
"avatar": "string", // url avatar (optional)
"dateOfBirth": "2023-10-06T02:43:24.154Z",
"password": "string",
"image": { // optional
"fileId": "string", // file id get from api upload(optional)
"fileName": "string", // name file (optional)
"extension": "string", // extension file
"fileUrl": "string", // url file
"contentType": "string"
},
"isAdmin": false // status is admin default false
}
// this way to get info user
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);
// param request
{
"GroupId": "string",
"Filters": "string", // string search
"phoneNumber": "string",
"Sorting": "ASC",
"SkipCount": 0,
"MaxResultCount": 0,
}
userDoAction
User action(add friend, unfriend, block , unblock)
const result = await chatUser.userDoAction(param);
// param request
{
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",// userid user
"actionType": 0 // 0: add friend, 1: unfriend, 2: block , 3 unblock
}
}
listFriends
const result = await chatUser.listFriends(param);
// param request
{
"GroupId": "string",
"Filters": "string", // string search
"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);
// param request
{
"userId": "string", // userId (can be null default get by token)
"groupName":"", // name group
"tagFilter": null, // number 1,2,3
"skip": 0, // point start record
"take": 0// The number of records retrieved
}
searchRoomChat
Searches for chat rooms based on specified criteria.
const result = await apiManage.searchRoomChat(param);
// param request
{
"roomType":"", // 0,1,2
"filter": '', // string filter
"skip": 0, // point start record
"take": 0// The number of records retrieved
}
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);
// param request
{
"skip": 0, // point start record
"take": 0// The number of records retrieved
}
getInfoRoomChat
Searches for chat rooms based on specified criteria.
const result = await apiManage.getInfoRoomChat(groupId);
// param request
groupId: ''; // id group chat
createRoomChat
Searches for chat rooms based on specified criteria.
const result = await apiManage.createRoomChat(param);
// param request
{
"name": "string", // name group
"roomType": 0, // deprecated
"typeOfGroup": 0, // type group 0.1.2, default 0
"topic": "string",
"description": "string",
"image": "string", // url image group
"ownerId": "string", // id user owner group
"participants": [
"string"
], // list userid
"tags": [
"string"
],
"sendDefaultMessage": true, //status send default message
"defaultMessageContent": "string" // message default
}
updateInfoRoom
Searches for chat rooms based on specified criteria.
const result = await apiManage.updateInfoRoom(param);
// param request
{
"name": "string",
"topic": "string",
"description": "string",
"image": "string"
}
deleteChatRoom
Searches for chat rooms based on specified criteria.
const result = await apiManage.deleteChatRoom(roomId);
// param request
"roomId":'' // room id
// response
// method delete success: 200 in header status
// error
joinChatGroup
Searches for chat rooms based on specified criteria.
const result = await apiManage.joinChatGroup(groupId, userId);
// param request
{
"groupId": "string",
"userId": "string"
}
inviteChatGroup
Searches for chat rooms based on specified criteria.
const result = await apiManage.inviteChatGroup(param);
// param request
{
"groupId": "string", // id room
"userId": [
"string"
] // list userId invite
}
inviteChatGroup
Searches for chat rooms based on specified criteria.
const result = await apiManage.removeUserChatGroup(param);
// param request
{
"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);
// param request
{
"before":"", //
"after": '', //messageid after
"isPined":null, //boolean, true message pined
"skip": 0, // point start record
"take": 0// The number of records retrieved
}
historyAttachment
Retrieves the history of attachments in a group.
const result = await apiMessage.historyAttachment(roomId, param);
// param request
{
"before":"", //
"after": '', //messageid after
"types": [], // - VIDEO = 1,AUDIO = 2,IMAGE = 3,FILE = 4,OTHER = 5
"skip": 0, // point start record
"take": 0// The number of records retrieved
}
searchMessegeInGroup
Retrieves the history of message in a group.
const result = await apiMessage.searchMessegeInGroup(roomId, param);
// param request
{
"textSearch": '', // text search
"skip": 0, // point start record
"take": 0// The number of records retrieved
}
getHistoryAroundMessage
Retrieves the history of attachments in a group.
const result = await api.getHistoryAroundMessage(param);
// param request
{
"roomId": '', // roomid search
"messageId": '', // messageid
"take": 10 // The number of records retrieved: default 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);
// url: http://123.30.145.67:14104/file/upload
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();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
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]);