1.7.2 • Published 3 months ago

@qte/nest-firebase-chat v1.7.2

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

@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 NameInput ParametersDescriptionReturns
getChatchatId: stringFetches a chat by its IDPromise<Chat & { chatId: string }>
getChatsByUseruserId: string, take?: number, skip?: number, searchQuery?: stringFetches a list of chats for a user with paginationPromise<(Chat & { chatId: string })[]>
getChatByUserCountuserId: stringFetches the count of chats for a userPromise<{ count: number }>
getUnreadChatByUserCountuserId: stringFetches the count of unread chats for a userPromise<{ count: number }>
createChatuserId: string, name?: string, userIds: string[]Creates a new chatPromise<string>
deleteChatchatId: stringDeletes a chat by its IDPromise<void>
getMessagechatId: string, messageId: stringFetches a message from a chat by its IDPromise<Message & { messageId: string }>
getMessageschatId: string, take?: number, skip?: numberFetches messages from a chat with paginationPromise<(Message & { messageId: string })[]>
getMessageCountchatId: stringFetches the count of messages in a chatPromise<{ count: number }>
sendMessagechatId: string, userId: string, content?: string, files?: string[]Sends a message in a chatPromise<string>
updateMessagechatId: string, messageId: string, content?: string, files?: string[]Updates a message in a chatPromise<Message & { messageId: string }>
markMessageAsReadchatId: string, userId: string, messageId: stringMarks a message as readPromise<Message & { messageId: string }>
markMessagesAsReadchatId: string, userId: string, messageIds: string[]Marks multiple messages as readPromise<(Message & { messageId: string })[]>
deleteMessagechatId: string, messageId: stringDeletes a message from a chatPromise<void>
createUseruserId: string, name: string, avatar?: stringCreates a new userPromise<string>
clearUseruserId: stringClears a user's dataPromise<void>
addUserToChatchatId: string, userId: stringAdds a user to a chatPromise<Chat & { chatId: string }>
addUsersToChatchatId: string, userIds: string[]Adds multiple users to a chatPromise<Chat & { chatId: string }>
removeUserFromChatchatId: string, userId: stringRemoves a user from a chatPromise<Chat & { chatId: string }>
removeUsersFromChatchatId: string, userIds: string[]Removes multiple users from a chatPromise<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!

1.7.2

3 months ago

1.7.1

3 months ago

1.7.0

3 months ago

1.6.9

5 months ago

1.6.11

5 months ago

1.6.10

5 months ago

1.6.12

5 months ago

1.6.8

5 months ago

1.6.7

5 months ago

1.2.0

11 months ago

1.6.3

9 months ago

1.6.2

9 months ago

1.5.3

10 months ago

1.6.1

10 months ago

1.5.2

10 months ago

1.6.0

10 months ago

1.5.1

10 months ago

1.5.0

10 months ago

1.4.0

10 months ago

1.1.3

11 months ago

1.3.0

10 months ago

1.2.1

10 months ago

1.1.2

11 months ago

1.5.3-beta.1

10 months ago

1.5.1-beta.1

10 months ago

1.5.0-beta.2

10 months ago

1.5.0-beta.1

10 months ago

1.5.3-beta.2

10 months ago

1.5.2-beta.2

10 months ago

1.5.2-beta.1

10 months ago

1.3.0-beta.1

10 months ago

1.3.0-beta.2

10 months ago

1.6.2-beta.1

9 months ago

1.1.4-beta.1

11 months ago

1.1.3-beta.1

11 months ago

1.4.0-beta.5

10 months ago

1.4.0-beta.4

10 months ago

1.6.6

5 months ago

1.4.0-beta.3

10 months ago

1.6.5

6 months ago

1.4.0-beta.2

10 months ago

1.4.0-beta.1

10 months ago

1.1.1

11 months ago

1.1.0

11 months ago

1.0.0

11 months ago