@qte/nest-firebase-chat
v1.7.3
Published
@qte/nest-firebase-chat is a powerful NestJS module that facilitates the integration of chat functionality in your NestJS applications using Firebase Firestore.
Downloads
61
Keywords
Readme
@qte/nest-firebase-chat
@qte/nest-firebase-chat is a powerful NestJS module that facilitates the integration of chat functionality in your NestJS applications using Firebase Firestore.
Prerequisites
Before proceeding, ensure that Firestore is enabled in your Firebase project and you have created a service account with Firestore Admin permissions.
Installation
Use npm to install the module:
npm install @qte/nest-firebase-chat
How to Use
Step 1: Import the NestFirebaseChatsModule
Import the NestFirebaseChatsModule
into your root module (e.g., AppModule):
import { Module } from '@nestjs/common'
import { AppController } from './app.controller'
import { AppService } from './app.service'
import { ConfigModule } from '@nestjs/config'
import { NestFirebaseChatsModule } from '@qte/nest-firebase-chat'
import config from './config/configuration'
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [config],
}),
NestFirebaseChatsModule.forRootAsync({
imports: [ConfigModule],
useFactory: () => ({
credentials: {
projectId: process.env.FIREBASE_PROJECT_ID,
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
privateKey: process.env.FIREBASE_PRIVATE_KEY,
},
}),
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Step 2: Utilize NestFirebaseChatsService
You can use the NestFirebaseChatsService
within your service or controller to interact with the chat functionality:
import { Body, Controller, Param, Get } from '@nestjs/common'
import { NestFirebaseChatsService } from '@qte/nest-firebase-chat'
@Controller()
export class AppController {
constructor(private firebaseChats: NestFirebaseChatsService) {}
@Get('chats/:chatId')
getChat(@Param('chatId') chatId: string) {
return this.firebaseChats.getChat(chatId)
}
// Add more routes as needed
}
Firestore Collection Schema
Ensure your Firestore collection structure aligns with the following schema for the module to operate correctly.
├── users
│ └── userId
│ ├── name (string)
│ └── avatar (URL string)
└── chats
└── chatId
├── creatorId (string)
├── creatorRef (reference to user document)
├── memberIds (array of references to user documents)
├── searchIndex (string concatenated of member names and chat name)
├── name (string)
├── lastMessage (reference to message document)
├── createdAt (ISO string)
├── updatedAt (ISO string)
├── members
│ └── memberId
│ ├── memberRef (reference to user document)
│ └── createdAt (ISO string)
└── messages
└── messageId
├── senderId (string)
├── senderRef (reference to user document)
├── content (string)
├── readByIds (array of references to user documents)
├── createdAt (ISO string)
├── updatedAt (ISO string)
└── files
└── fileId
├── url (URL string)
└── createdAt (ISO string)
Features
The library provides a robust set of functions for creating a backend chat service using Firestore.
| Method Name | Input Parameters | Description | Returns |
| -------------------------- | ----------------------------------------------------------------------- | -------------------------------------------------- | ---------------------------------------------- |
| getChat
| chatId: string
| Fetches a chat by its ID | Promise<Chat & { chatId: string }>
|
| getChatsByUser
| userId: string, take?: number, skip?: number, searchQuery?: string
| Fetches a list of chats for a user with pagination | Promise<(Chat & { chatId: string })[]>
|
| getChatByUserCount
| userId: string
| Fetches the count of chats for a user | Promise<{ count: number }>
|
| getUnreadChatByUserCount
| userId: string
| Fetches the count of unread chats for a user | Promise<{ count: number }>
|
| createChat
| userId: string, name?: string, userIds: string[]
| Creates a new chat | Promise<string>
|
| deleteChat
| chatId: string
| Deletes a chat by its ID | Promise<void>
|
| getMessage
| chatId: string, messageId: string
| Fetches a message from a chat by its ID | Promise<Message & { messageId: string }>
|
| getMessages
| chatId: string, take?: number, skip?: number
| Fetches messages from a chat with pagination | Promise<(Message & { messageId: string })[]>
|
| getMessageCount
| chatId: string
| Fetches the count of messages in a chat | Promise<{ count: number }>
|
| sendMessage
| chatId: string, userId: string, content?: string, files?: string[]
| Sends a message in a chat | Promise<string>
|
| updateMessage
| chatId: string, messageId: string, content?: string, files?: string[]
| Updates a message in a chat | Promise<Message & { messageId: string }>
|
| markMessageAsRead
| chatId: string, userId: string, messageId: string
| Marks a message as read | Promise<Message & { messageId: string }>
|
| markMessagesAsRead
| chatId: string, userId: string, messageIds: string[]
| Marks multiple messages as read | Promise<(Message & { messageId: string })[]>
|
| deleteMessage
| chatId: string, messageId: string
| Deletes a message from a chat | Promise<void>
|
| createUser
| userId: string, name: string, avatar?: string
| Creates a new user | Promise<string>
|
| clearUser
| userId: string
| Clears a user's data | Promise<void>
|
| addUserToChat
| chatId: string, userId: string
| Adds a user to a chat | Promise<Chat & { chatId: string }>
|
| addUsersToChat
| chatId: string, userIds: string[]
| Adds multiple users to a chat | Promise<Chat & { chatId: string }>
|
| removeUserFromChat
| chatId: string, userId: string
| Removes a user from a chat | Promise<Chat & { chatId: string }>
|
| removeUsersFromChat
| chatId: string, userIds: string[]
| Removes multiple users from a chat | Promise<Chat & { chatId: string }>
|
License
This module is licensed under the MIT License.
Issues
If you encounter any problems or have feature suggestions, please create an issue.
Get started with integrating Firebase Firestore chat into your NestJS application with @qte/nest-firebase-chat. Happy coding!