Skip to content

API reference / @chanx-js/client

@chanx-js/client ​

Type-safe WebSocket client for TypeScript. Generate it from a server's AsyncAPI schema, and every message you send and receive is typed: in the browser, React, Vue, Svelte, Solid, or another Node service. No runtime dependencies.

Documentation · Getting started · Why chanx-js? · API

bash
npm install @chanx-js/client
npm install -D @chanx-js/codegen
npx chanx-codegen http://localhost:8000/asyncapi.json   # writes src/generated
ts
import { createClient } from '@chanx-js/client';

import { chat } from './src/generated/index.js';

const client = createClient({ baseUrl: 'ws://localhost:8000' });
const connection = client.connect(chat, { params: { room: 'lobby' } });

connection.on('notice', (message) => console.log(message.payload.text));
const pong = await connection.request({ action: 'ping' }); // typed: PongMessage
connection.send({ action: 'chat', payload: { text: 'hello' } });
  • Typed both ways: a misspelt action or field is a compile error, and request() resolves with the reply its action declares.
  • Topics: many subscriptions over one socket, restored after a reconnect.
  • Reconnects and heartbeats built in, and sockets shared safely between components.
  • Works with any chanx server, Node or Python.
ImportFor
@chanx-js/clientThe runtime: clients, connections, topics
@chanx-js/client/reactuseChannel, useTopic, useTopics
@chanx-js/client/vueComposables returning refs
@chanx-js/client/svelteStores, for Svelte 4 and 5
@chanx-js/client/solidSignals and accessors

MIT licensed.

Modules ​

MIT licensed