@fiveminutes-io/chat-client
v1.0.2
Published
Client library for Five Minute Chat with SignalR integration, authentication, and message handling
Maintainers
Readme
FiveMinutes Chat Client
High-level TypeScript client library for FiveMinutes Chat with built-in SignalR integration, automatic authentication, and event-driven message handling.
Installation
npm install @fiveminutes-io/chat-clientThis package also requires the contracts package:
npm install @fiveminutes-io/five-minute-chat-contractsQuick Start
import { ChatClient } from "@fiveminutes-io/chat-client";
// Initialize client
const client = new ChatClient({
serverUrl: "https://signalr.fiveminutes.cloud/signalr",
applicationId: "YOUR_APP_ID",
applicationSecret: "YOUR_SECRET",
username: "MyUsername",
});
// Setup event handlers
client.onConnected(() => console.log("Connected!"));
client.onDisconnected((error) => console.log("Disconnected", error));
client.onMessageReceived((msg) => console.log(`${msg.sender}: ${msg.content}`));
client.onError((error) => console.error("Error:", error));
// Connect
await client.connect();
// Send message
await client.sendMessage("Hello, World!");
// Cleanup
await client.disconnect();Configuration
interface ChatClientConfig {
/** SignalR hub URL */
serverUrl: string;
/** Application ID for authentication */
applicationId: string;
/** Application secret for authentication */
applicationSecret: string;
/** Username to use in the chat */
username: string;
/** Optional: Auto-join channel on connect (default: "Global") */
autoJoinChannel?: string;
/** Optional: Unique device ID (auto-generated if not provided) */
deviceId?: string;
/** Optional: System language (defaults to navigator.language) */
language?: string;
/** Optional: Runtime platform (defaults to "Web") */
platform?: string;
}API
Methods
- connect(): Connect to the chat server
- disconnect(): Disconnect from the chat server
- sendMessage(content, channel?): Send a message to a channel
- isConnectedToServer(): Check if currently connected
- getConnectionState(): Get the current connection state
Event Callbacks
- onConnected(callback): Called when connection is established
- onDisconnected(callback): Called when connection is lost
- onMessageReceived(callback): Called when a message is received
- onError(callback): Called when an error occurs
Message Event
interface ChatMessageEvent {
messageId: string;
content: string;
sender: string;
timestamp: Date;
channel?: string;
metadata?: Record<string, string>;
}Features
- Automatic Authentication: Handles the SignalR handshake automatically
- Event-Driven: Simple callback-based API for handling connection and messages
- Error Handling: Built-in error handling and reconnection support
- TypeScript Support: Fully typed for a great development experience
- Lightweight: Minimal dependencies, only requires @microsoft/signalr
