firebase-chat-ready-api v3.0.12
Firebase chat API 🔥
Firebase-chat-ready-API is a simple package enable fast connection to firebase firestore to create chat manager. Create chat rooms, send messages and listen to instance messages .
Getting started in a minute
Installation with
$ npm install firebase-chat-ready-apiUsage
This package have two main classes
ChatRoomclass andMessageclass as theChatRoomclass have properties and functions related to the whole chat room such as title, get messages, sending messages, etc ...and the
Messageclass have properties and functions related to individual message like message body, time of creation, update message, etc ...
First, import it
No ES6
const {
initializeFirebase,
ChatRoom
} = require("firebase-chat-ready-api");ES6
import { initializeFirebase, ChatRoom } from "firebase-chat-ready-api";Initialize the app
initializeFirebase({
// your firebase app config...
});Create chat room between two users
// define users objects
let userA = {userId: '1'}
let userB = {userId: '2'}
// create chat room
let newchatRoom = new ChatRoom("chat title", userA, userB, err => {
if (!err) console.log(" chat room created successfully");
});This class take 5 params as initial values
titlethe chat room titleuserAone of the chat room members. object of{userId, username, photo}userBsecond chat member. object of{userId, username, photo}onCompleteit's a callback function called after the chat room created successfully in firebasefromRefcreate a class for chat room with it's firebase reference
Note: you can get created chat props also in the callback function through:
var newchatRoom = new ChatRoom("chat title", userA, userB, err => { // get the chat key if (!err) console.log(newchatRoom.chatRoomRef.key); });
User Object is consists of
userId: the user unique id requiredusename: username for this user not requiredphoto: url image for this user not required
Note: you can get all of title, members with their props and createdAt bt simply user the chat instance
newchatRoom.members[0].username; newchatRoom.ctreatedAt; ...
Change the chat title
newchatRoom.setNewTitle("new title", title => {
console.log("the chat new title is " + title);
});This method is property of ChatRoom class. call with 2 params
titleas a new string represent the new titleonCompletecallback after changing the title passing the new title
Remove chat room
newchatRoom.remove();This method is property of ChatRoom class. call with 2 params
softRemoveset flagisRemovedtotrueonCompletecallback after removing the chat room
Remove mutual chat rooms between two users
ChatRoom.removeMutualChatRooms('userA Id', 'userB Id);This method static function call with 2 params
userAThe id of the user AuserBThe id of the user BsoftRemoveset flagisRemovedtotrue
Send message in this chat room
You can use the ChatRoom instances to send messages to it.
var message = newchatRoom.sendMessage("Hi", userA, err => {
if (!err) console.log("message sent");
});This method also member of ChatRoom class. with 3 params
bodystring is the message bodyfromrepresent the user how send the message could be user Id or the user objectonCompletecallback after sending the message to the firbase
this method is return Message instance
Get chat rooms related to user
ChatRoom.getUserChatRooms(userB, (err, chats) => {
if (!err) console.log("Count of chats is :", chats.length);
chats.map(chat => {
console.log(chat.members[0].username);
});
});Note: This method is a
staticfunction
call with 2 params
userthe user could be user Id or the user objectonCompletecallback function call after receiving all chats from firebase passing 2 paramserris the error message if the call failedchatsit's an array ( List ) of all the user chat room (ChatRoominstances)
Get chat messages and listen for new messages comming
newchatRoom.getMessagesAndListen(message => {
console.log(message.body);
});call 1 params
actioncallback function is the action that should happen when receiving a messageNote : the massages come one after one not in list
This function fires after getting new message
Update message
message.updateBody("new message", newBody => {
console.log("message text updated to" + newBody);
});This method member of Message class
call with 2 params
newBodyis the new updated messageonCompletecallback after update
Remove message
var removedMessage = message.remove();This method member of Message class
return the deleted message
call with 1 params
newMessageis the new updated message stringafterRemovecallback after removing the message
You can get CreatedAt and updatedAt timestamps by message instance
console.log(message.createdAt);
// as the updatedAt property not available only after the message get updated
if (message.updatedAt) {
console.log(message.updatedAt);
}return timestamp as you can easily format it using package like moment (for more formats)
or by simply use Date Class
var date = new Date(createdAt);
// like: 4:01:50 AM
console.log(date.toLocaleTimeString());
// like: 10/16/2018
console.log(date.toLocaleDateString());
// like: 110/16/2018, 4:01:50 AM
console.log(date.toLocaleString());
createdAtproperty available also inChatRoominstance
You can get also the chat room firebase reference(ref) and key. Like this :
var reference = newchatRoom.chatRoomRef;
var key = newchatRoom.chatRoomRef.key;as newchatRoom is an instance of ChatRoom Class
Find chat by key (uid)
staticfunction
ChatRoom.findById("-LPK1Rr5mzwkuSDV9U9a", (err, chat) => {
if (!err) console.log(chat);
});call with 2 params
uidchat unique id (key)onCompletecallback function call after receiving all chats from firebase passing 2 paramserris the error message if the call failedchatchat room (ChatRoominstance)
Tests
Tests are using Jest, to run the tests add your firebase config object in the test file and run:
$ npm testRoadmap
Check out our roadmap to get informed by the latest feature released and the upcoming ones. You can also give us insights and vote for a specific feature. And your are more than welcome to contribute.
Note: You probably should change the rules of the firbase to link it correctly
👀 see examples.js