Firebase chat API 🔥
Firebase-chat-ready-API is a simple package enable fast connection to firebase realtime database and create chat manager. Create chat rooms, send messages and listen to instance messages .
Getting started in a minute
Installation with
$ npm install @nezam/firebase-chat-ready-api
Usage
This package have two main classes ChatRoom
class and Message
class as the ChatRoom
class have properties and functions related to the whole chat room such as title, get messages, sending messages, etc ...
and the Message
class 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("@nezam/firebase-chat-ready-api");
ES6
import { initializeFirebase, ChatRoom } from "@nezam/firebase-chat-ready-api"
Initialize the app
var config = {
};
initializeFirebase(config);
Create chat room between two users
var chatRoomOne = new ChatRoom("chat title", "user1233", "user1234", err => {
if (!err) console.log(" chat room created successfully");
});
This class take 5 params as initial values
title
the chat room titleuserA
the one of the members "id" of the chatuserB
the other member "id"onComplete
it's a callback function called after the chat room created successfully in firebasefromRef
create a class for chat room with it's firebase reference
Change the chat title
chatRoomOne.setNewTitle("new title", title => {
console.log("the chat new title is " + title);
});
This method is property of ChatRoom
class. this method call with 2 params
title
as a new string represent the new titleonComplete
callback after changing the title passing the new title
Send message in this chat room
You can use the ChatRoom instances to send messages to it.
var message = chatRoomOne.sendMessage("Hi", userId, err => {
if (!err) console.log("message sent");
});
This method also member of ChatRoom
class. as it call with 3 params
body
string is the message bodyfrom
represent the user how send the messageonComplete
callback after sending the message to the firbase
this method is return Message
instance
Get chat rooms related to user
ChatRoom.getUserChatRooms(userId, (err, chats) => {
if (!err) console.log("Count of chats is :", chats.length);
chats.map(chat => {
console.log(chat.title);
});
});
Note: This method is a static
function
This method call with 2 params
-
userId
as it's the user \id
-
onComplete
callback function call after receiving all chats from firebase passing 2 params
err
is the error message if the call failedchats
it's an array ( List ) of all the user chat room (ChatRoom
instances)
Get chat messages and listen for new messages comming
chatRoomOne.getMessagesAndListen(message => {
console.log(message.body);
});
This method call 1 params
-
action
callback function is the action that should happen when receiving a message
Note : 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
newBody
is the new updated messageonComplete
callback after update
Remove message
var removedMessage = message.remove();
This method member of Message
class
this method return the deleted message
call with 1 params
newMessage
is the new updated message stringafterRemove
callback after removing the message
You can get CreatedAt and updatedAt timestamps by message instance
console.log(message.createdAt);
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);
console.log(date.toLocaleTimeString())
console.log(date.toLocaleDateString())
console.log(date.toLocaleString())
createdAt
property available also in ChatRoom
instance
Tests
Tests are using Jest, to run the tests add your config object in the test file and run:
$ npm test
Roadmap
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 linked correctly
👀 see examples.js