@dipen557/chat-sdk
v1.0.0
Published
WebSocket-based chat system SDK for 100xdevs
Downloads
67
Readme
Chat SDK Documentation
This SDK provides a simple interface to interact with the WebSocket-based chat system.
1. Server Setup
Ensure your WebSocket server is running and accessible. The server should be configured with:
- WebSocket endpoint at
/ws
- JWT authentication
- Message history support
- Room management
Installation
# If using npm
npm install @dipen557/chat-sdk
# If using yarn
yarn add @dipen557/chat-sdk
Basic Usage
import { ChatSDK } from '@dipen557/chat-sdk';
// Initialize the SDK
const chatSDK = new ChatSDK({
serverUrl: 'ws://your-server-url',
token: 'your-jwt-token',
onError: (error) => console.error('Chat error:', error),
onConnectionChange: (connected) => console.log('Connection status:', connected)
});
// Connect to the chat server
await chatSDK.connect();
// Listen for messages
chatSDK.on('message', (message) => {
console.log('New message:', message);
});
// Listen for room access updates
chatSDK.on('roomAccess', (roomIds) => {
console.log('Available rooms:', roomIds);
});
// Join a room
chatSDK.joinRoom('room-id');
// Send a message
chatSDK.sendMessage('room-id', 'Hello, world!');
// Disconnect when done
chatSDK.disconnect();
React Integration Example
import { useEffect, useState } from 'react';
import { ChatSDK, Message } from '@dipen557/chat-sdk';
function ChatComponent() {
const [messages, setMessages] = useState<Message[]>([]);
const [sdk, setSDK] = useState<ChatSDK | null>(null);
useEffect(() => {
const chatSDK = new ChatSDK({
serverUrl: 'ws://your-server-url',
token: 'your-jwt-token'
});
chatSDK.on('message', (message) => {
setMessages(prev => [...prev, message]);
});
chatSDK.connect().catch(console.error);
setSDK(chatSDK);
return () => {
chatSDK.disconnect();
};
}, []);
const sendMessage = (content: string) => {
sdk?.sendMessage('room-id', content);
};
return (
<div>
{messages.map(msg => (
<div key={msg.id}>{msg.content}</div>
))}
</div>
);
}
Events
The SDK emits the following events:
message
: Fired when a new message is receivedroomAccess
: Fired when room access information is receivedhistory
: Fired when message history is received after joining a roomerror
: Fired when an error occursconnectionChange
: Fired when the connection status changes
API Reference
Constructor
new ChatSDK(options: ChatSDKOptions)
Options:
serverUrl
: WebSocket server URLtoken
: JWT authentication tokenonError?
: Error callback functiononConnectionChange?
: Connection status callback function
Methods
connect(): Promise
Connects to the chat server.
joinRoom(roomId: string): void
Joins a specific chat room.
sendMessage(roomId: string, content: string): void
Sends a message to a specific room.
disconnect(): void
Disconnects from the chat server.
Error Handling
The SDK includes automatic reconnection logic with exponential backoff. It will attempt to reconnect up to 5 times when the connection is lost.
TypeScript Support
The SDK is written in TypeScript and includes type definitions for all interfaces and methods.
Security Considerations
- Always use HTTPS in production
- Never expose JWT tokens in client-side code
- Implement proper token refresh mechanisms
- Set appropriate WebSocket timeouts
- Implement rate limiting on the server side
Browser Support
The SDK supports all modern browsers that implement the WebSocket API:
- Chrome 6+
- Firefox 6+
- Safari 7+
- Edge 12+
- Opera 12.1+