Chat Adapter
The chat adapter implements a real-time socket-based messaging services between users, using MongoDB as a data store.
Features
- Real-time messaging
- Users send and receive messages from eachother in real time.
- Topic management
- Topics can be created and organized to group chatting targets. For example, a topic can be created to organize messages around an event or theme.
- Subscription management
- Users can choose to subscribe to a specific topic. This lets them participate in topics relevant to them, and take part in real-time discussions.
- Storing message history
- Message history can be saved, and users can view and search previous messages.
Installation
- Prerequisites
Dependency | Version |
---|---|
node | 18+ |
MongoDB | 5+ |
Nodeblocks User Service | 2.4.1+ |
Nodeblocks Org Service | 2.1.2+ |
- Install Package
Create your repository and add this package as a dependency
mkdir my-chat-service
npx gts init -y
npm install --save @basaldev/blocks-messaging-service
You will need to also set up your environment variables. Look at Quick Start Guide for a sample.
- Initial code
info
This example enables CORS whitelist for localhost. You can add your own domains to the array.
corsOptions: {
credentials: true,
origin: ['http://localhost', 'http://your-domain.com'],
},
Place the following into src/index.ts:
import {
defaultChat,
createNodeblocksMessagingApp,
} from '@basaldev/blocks-messaging-service';
import {security} from '@basaldev/blocks-backend-sdk';
async function main() {
let adapter = await defaultChat.createChatDefaultAdapter(
{
allowedAttachmentContentTypes: ['jpeg', 'png'],
authEncSecret: getEnvString('AUTH_ENC_SECRET', ''),
authSignSecret: getEnvString('AUTH_SIGN_SECRET', ''),
authenticate: security.defaultCookieAuth,
maxAttachmentSizeMB: Number(getEnvString('MAX_FILE_SIZE_MB', '10')),
},
{
bucket: getEnvString('BUCKET_NAME', ''),
db: getEnvString('DATABASE_URL', ''),
organizationAPI: getEnvString('ORGANIZATION_ENDPOINT', ''),
socketAPI: 'socketIO',
userAPI: {
admin: getEnvString('ADMIN_USER_ENDPOINT', ''),
expand: getEnvString('DEMAND_USER_ENDPOINT', ''),
},
}
);
const chatApp = createNodeblocksMessagingApp({
corsOptions: {
credentials: true,
origin: ['http://localhost'],
},
enableCookieParser: true,
});
const chatServer = await chatApp.startService({
PORT: Number(getEnvString('PORT', '8080')),
adapter,
env: 'development',
});
await (
adapter.dependencies.socketAPI as defaultChat.DefaultChatSocketIOApi
).startServer({
env: 'development',
server: chatServer,
});
}
void main();