faatlab-chat-engine
v1.0.0
Published
chat-engine
Downloads
3
Readme
📦 Chat Engine
🕸️ Installation
npm install @faatlab/chat-engine
🛠️ Usage
Initial setup
import { ChatEngine } from "chat-engine";
// Create HTTP server
const server = http.createServer(app);
const chat_engine = new ChatEngine(
{
database_url: "mysql://user:user@123@localhost:3306/chat-engine",
dialect: "mysql",
},
server
);
await chat_engine.connect();
Passing server to chat engine is optional. If you want to use only the chat engine methods and not the socket methods, pass the database details as arguments
const chat_engine = new ChatEngine({
database_url: "mysql://user:user@123@localhost:3306/chat-engine",
dialect: "mysql",
});
await chat_engine.connect();
Chat Engine Methods
createNewUser
const user = await chat_engine.createNewUser({
user_id: "123456",
name: "hulk",
thumbnail: "path/user_profile.png",
metadata: {
email: "[email protected]",
phone: "9876543210",
},
});
// thumbnail and metadata are optional. metadata can be any json object.
console.log("User: ", user);
createBulkUsers
const users = await chat_engine.createBulkUsers([
{
user_id: "1011",
name: "tony stark",
thumbnail: "path/user_profile.png",
},
{
user_id: "1012",
name: "batman",
},
{
user_id: "1013",
name: "deadpool",
metadata: {
email: "[email protected]",
phone: "9876543210",
},
},
{
user_id: "1014",
name: "captain america",
},
]);
getUserAuthToken
const token = await chat_engine.getUserAuthToken("123456");
console.log("Token: ", token);
createNewChat
const chat = await chat_engine.createNewChat({
sender_id: "123456",
receiver_id: "101178",
message: "Hello",
metadata: {
additional_info: "json_data",
},
});
// metadata is optional and can be any json object
console.log("Chat: ", chat);
createBulkChats
const bulk_chats = await chat_engine.createBulkChats([
{ sender_id: "1011", receiver_id: "1012", message: "Hello World! 1" },
{ sender_id: "1012", receiver_id: "1013", message: "Hello World! 2" },
{ sender_id: "1013", receiver_id: "1014", message: "Hello World! 3" },
{ sender_id: "1012", receiver_id: "1011", message: "Hello World! 4" },
]);
listChatUsers
const users = await chat_engine.listChatUsers({
user_id: "1011",
page: 0,
size: 10,
});
console.log(users);
getChatsBetweenUsers
const chats = await chat_engine.getChatsBetweenUsers({
user1_id: "1011",
user2_id: "1012",
seen: true,
page: 0,
size: 10,
});
// argument 'seen' is optional. pass argument 'seen:true' for receivers,
// to mark the chats has been seen by the receiver
console.log(chats);
markChatAsSeen
const result = await chat_engine.markChatAsSeen(
user1_id: "1011",
user2_id: "1012",
);
console.log(result);
Web Socket Methods
user:connect
socket.emit("user:connect");
send:personal_chat
socket.emit("send:personal_chat", { message, receiver_id });
receive:personal_chat
socket.on("receive:personal_chat", {(...args) => {
// ...
});
error
socket.on("error", {(...args) => {
// ...
});