@metric-im/chat-server
v1.0.0
Published
Chat module for metric-im with channels and direct messages
Readme
Chat Server
A Slack-like chat module for metric-im with channels and direct messages.
Features
- Channels: Create public or private channels for team communication
- Direct Messages: One-on-one private conversations
- Real-time Updates: Messages are polled every 2 seconds for new content
- Message Management: Edit and delete your own messages
- Member Management: Add and remove members from channels
- Authentication: Integrated with epistery for user identity and authentication
Installation
cd ~/workspace/metric-im/chat-server
npm installUsage
Server-side Integration
import ChatServer from '@metric-im/chat-server';
// In your componentry initialization
await componentry.init(
// ... other modules
ChatServer
);Client-side Usage
import Chat from '/components/Chat.mjs';
// Render the chat interface
const chat = new Chat({
context: {
userId: 'user-id',
id: 'account-id'
}
});
await chat.render(document.body);API Endpoints
Channels
GET /chat/channels- Get all channels the user has access toPOST /chat/channel- Create a new channel- Body:
{ name, description, type }
- Body:
GET /chat/channel/:channelId- Get channel details and membersPOST /chat/channel/:channelId/member- Add a member to a channel- Body:
{ userId }
- Body:
DELETE /chat/channel/:channelId/member/:userId- Remove a member from a channel
Direct Messages
GET /chat/dms- Get all DM conversationsPOST /chat/dm- Create or get existing DM with a user- Body:
{ targetUserId }
- Body:
Messages
GET /chat/messages/:channelId- Get messages from a channel- Query params:
limit(default: 50),before(timestamp)
- Query params:
POST /chat/message- Send a message- Body:
{ channelId, text }
- Body:
PUT /chat/message/:messageId- Edit a message- Body:
{ text }
- Body:
DELETE /chat/message/:messageId- Delete a message
Data Structures
Channel
{
_id: String,
name: String,
description: String,
type: 'channel' | 'public' | 'private' | 'dm',
dmParticipants: [String], // Only for DMs
accountId: String,
createdBy: String,
createdAt: Date,
modifiedAt: Date
}Message
{
_id: String,
channelId: String,
userId: String,
text: String,
createdAt: Date,
modifiedAt: Date,
edited: Boolean
}Member
{
_id: String,
channelId: String,
userId: String,
joinedAt: Date
}Components
Chat
Main component that renders the entire chat interface with sidebar and chat window.
ChatSidebar
Left sidebar showing channels and DMs list.
ChatWindow
Main chat area showing messages and input field.
Authentication
The module uses epistery for authentication. All endpoints require an authenticated user with req.account.id and req.account.userId.
The authentication middleware is applied to all /chat/* routes:
router.use('/chat', (req, res, next) => {
if (req.account && req.account.id) next();
else res.status(401).send();
});Database Collections
The module uses three MongoDB collections:
chat_channels- Stores channel informationchat_messages- Stores all messageschat_members- Stores channel membership
Indexes are automatically created on initialization for optimal query performance.
Styling
The module includes a CSS file at assets/chat.css that provides a complete styling solution. The styles use CSS variables for theming:
--background-secondary--border-color--hover-background--active-background--text-muted--primary-color--primary-hover--spacer--spacerhalf
Development
Based on the wiki-mixin structural template, the chat-server follows the same patterns:
- Server module extends
Componentry.Module - Client components extend
Component - Uses epistery for authentication
- Follows the componentry framework conventions
Future Enhancements
Potential improvements for future versions:
- WebSocket support for true real-time messaging
- File uploads and image sharing
- Message reactions and threading
- User presence indicators
- Typing indicators
- Search functionality
- Message pinning
- Notifications
- Rich text formatting
