@forgeframework/ws-client
v0.3.0
Published
WebSocket client for the Forge Framework.
Maintainers
Readme
@forgeframework/ws-client
WebSocket client for the Forge Framework.
Part of the Forge Framework — a TypeScript framework for backend microservices and web applications.
Installation
npm install @forgeframework/ws-clientUsage
import { WebSocketClientFactory } from '@forgeframework/ws-client';
const factory = new WebSocketClientFactory();
const client = factory.create({
url: 'wss://realtime.example.com/ws',
headers: {
Authorization: 'Bearer eyJhbGciOiJSUzI1NiJ9...',
},
reconnect: {
enabled: true,
maxRetries: 5,
backoff: { initial: 1000, max: 30000, multiplier: 2 },
onReconnect: () => resubscribeAll(),
},
heartbeat: {
enabled: true,
interval: 5000,
timeout: 10000,
},
});
client.on('connect', () => {
console.log('Connected');
});
client.on('disconnect', (code, reason) => {
console.log(`Disconnected: ${code} ${reason}`);
});
client.on('reconnecting', (attempt) => {
console.log(`Reconnecting attempt ${attempt}`);
});
await client.connect();
const orderSub = client.subscribe<OrderUpdate>('orders', (message) => {
console.log(`Order ${message.data.orderId}: ${message.data.status}`);
});
const priceSub = client.subscribe<PriceUpdate>('prices', (message) => {
console.log(`${message.data.symbol}: ${message.data.price}`);
});
client.send('orders', { action: 'subscribe', symbols: ['AAPL', 'GOOG'] });
orderSub.unsubscribe();
await client.disconnect();Documentation
Full API reference and guides: https://carbonforge.io/framework/docs/clients/ws-client
(Documentation site launching soon.)
License
MIT © Carbon Forge
