0.2.43 • Published 3 months ago

chat-app-workhub v0.2.43

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

Welcome to realtimeChat with socket.io

This documentation provides an overview of the Chat , Networkclass 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

MethodDescription
connectCreate connections
disconnetStop connections
sendMessagesend message with group
updateMessageupdate message with group
receiveMessageListens for incoming mes
receiveDeleteMessageListens for incoming mes delete
setMessageSeenStatusSets 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

MethodParamsDescription
LoginuserName:String, passWord: stringget 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]);
0.2.43

3 months ago

0.2.41

3 months ago

0.2.40

3 months ago

0.2.42

3 months ago

0.2.39

3 months ago

0.2.38

3 months ago

0.2.37

3 months ago

0.2.36

3 months ago

0.2.35

4 months ago

0.2.34

4 months ago

0.2.33

4 months ago

0.2.32

4 months ago

0.2.31

4 months ago

0.2.26

4 months ago

0.2.25

4 months ago

0.2.30

4 months ago

0.2.29

4 months ago

0.2.24

4 months ago

0.2.23

4 months ago

0.2.22

4 months ago

0.2.21

4 months ago

0.2.20

4 months ago

0.2.19

4 months ago

0.2.18

4 months ago

0.2.17

4 months ago

0.2.16

4 months ago

0.2.15

4 months ago

0.2.14

4 months ago

0.2.13

4 months ago

0.2.12

4 months ago

0.2.11

4 months ago

0.2.10

4 months ago

0.2.7

4 months ago

0.2.6

4 months ago

0.2.9

4 months ago

0.2.8

4 months ago

0.2.3

4 months ago

0.2.5

4 months ago

0.2.4

4 months ago

0.1.15

4 months ago

0.2.1

4 months ago

0.2.0

4 months ago

0.1.16

4 months ago

0.2.2

4 months ago

0.1.14

5 months ago

0.1.13

5 months ago

0.1.10

5 months ago

0.1.11

5 months ago

0.1.12

5 months ago

0.1.9

5 months ago

0.1.8

5 months ago

0.1.7

5 months ago

0.1.4

5 months ago

0.1.6

5 months ago

0.1.5

5 months ago

0.1.3

5 months ago

0.1.2

5 months ago

0.1.1

5 months ago

0.1.0

5 months ago