@xhub-ui/chat-lite
v0.1.1
Published
Lightweight 1-on-1 chat SDK for XHub - zero dependencies on @xhub-chat/*
Maintainers
Readme
@xhub-ui/chat-lite
Lightweight 1-on-1 chat SDK for XHub. This package is intentionally small: it uses native fetch, Centrifuge for realtime, and does not depend on @xhub-chat/core or @xhub-chat/nextjs.
Install
pnpm add @xhub-ui/chat-liteUsage
Basic Usage
import { ChatLiteClient } from '@xhub-ui/chat-lite';
const client = new ChatLiteClient({
baseUrl: 'https://gw-messages.blocktrend.xyz',
accessToken: 'eyJhbGciOi...',
realtime: {
url: 'wss://centrifugo-sb-message.blocktrend.xyz/connection/websocket',
},
});
client.on('connected', () => {
console.log('Connected');
});
client.on('message', (message) => {
console.log('New message', message);
});
client.on('error', (error) => {
console.error(error.code, error.message);
});
await client.connect();
const rooms = await client.getRooms();
await client.sendMessage(rooms[0].id, 'Hello!');
const history = await client.getMessages(rooms[0].id, { limit: 20 });
console.log(history.messages);
const newRoom = await client.createRoom('user-002');
console.log(newRoom);
client.updateToken('new-access-token');
client.disconnect();Global Configuration
Set global config once, reuse across multiple client instances:
import { setGlobalConfig, ChatLiteClient } from '@xhub-ui/chat-lite';
// Set global defaults
setGlobalConfig({
baseUrl: 'https://gw-messages.blocktrend.xyz',
accessToken: 'eyJhbGciOi...',
realtime: {
url: 'wss://centrifugo-sb-message.blocktrend.xyz/connection/websocket',
},
});
// Create clients without passing a userId
const client1 = new ChatLiteClient({
baseUrl: 'https://gw-messages.blocktrend.xyz',
accessToken: 'eyJhbGciOi...',
});
const client2 = new ChatLiteClient({
baseUrl: 'https://gw-messages.blocktrend.xyz',
accessToken: 'eyJhbGciOi...',
});
// Instance options override global config
const client3 = new ChatLiteClient({
baseUrl: 'https://api-dev.xhub.vn', // Override global baseUrl
});Demo
A standalone demo application is available at packages/chat-lite-demo. Run it with:
cd packages/chat-lite-demo
pnpm install
pnpm devThe demo runs on port 3001 and demonstrates all chat-lite features including room management, messaging, and realtime updates.
API
new ChatLiteClient(options)
Required options:
baseUrl: XHub API base URL. The lite client targets/api/v1endpoints.accessToken: bearer token used for HTTP and, by default, realtime auth.
Optional options:
userId: current user ID, only needed when consumers wantuser:{userId}as a default realtime channel or local outgoing-message fallback metadata.realtime.url: Centrifuge websocket endpoint.realtime.initialToken: token passed to Centrifuge on construction. Defaults toaccessToken.realtime.defaultChannels: defaults to['online:index'], or['online:index', 'user:{userId}']whenuserIdis provided.realtime.autoStart: defaults totrue.useAuthorizationHeader: defaults totrue.fetchFn: custom fetch implementation.logLevel:debug,info,warn,error, orsilent.
Methods
connect(): Promise<void>disconnect(): voidsendMessage(roomId, body): Promise<Message>getMessages(roomId, options?): Promise<MessagesResponse>createRoom(withUserId): Promise<Room>sendsparticipants: [withUserId].getRooms(): Promise<Room[]>getRealtime(): CentrifugeLiteRealtime | undefinedupdateToken(token): void
Events
connecteddisconnectedreconnectingmessageerrorrealtime.connectedrealtime.disconnectedrealtime.reconnectingrealtime.errorrealtime.message.newrealtime.room.updatedrealtime.presence
