open-index/open-npm huggingface dataset:
npm package: @jaewu/client
Description: WebSocket client for real-time topic subscriptions and price updates. Works in the browser (native `WebSocket`) and in Node or tests if you supply `createWebSocket`. Optional React helpers live under `@jaewu/client/react`; add `react` ≥ 18 only if you use
Latest version: 0.0.3

# `@jaewu/client`

WebSocket client for real-time topic subscriptions and price updates. Works in the browser (native `WebSocket`) and in Node or tests if you supply `createWebSocket`. Optional React helpers live under `@jaewu/client/react`; add `react` ≥ 18 only if you use them.

## Get started

```bash
npm install @jaewu/client
```

```ts
import { StreamClient } from "@jaewu/client";

const client = new StreamClient({ url: "wss://your-gateway.example/ws" });

await client.connect();
client.setSubscriptions(["stock.AAPL"]);

const price = client.getStore().get("stock.AAPL");
// …use `price` or subscribe to the store / connection state (see examples)

client.disconnect();
```

## Examples

### Subscriptions and reading prices

```ts
import { StreamClient } from "@jaewu/client";

const client = new StreamClient({
  url: "wss://your-gateway.example/ws",
  clientId: "optional-id",
  authToken: "optional-token",
});

await client.connect();
client.setSubscriptions(["stock.AAPL", "stock.MSFT"]);
client.addSubscriptions(["stock.GOOGL"]);
client.removeSubscriptions(["stock.MSFT"]);

const store = client.getStore();
const unsubscribe = store.subscribe(() => {
  console.log(store.snapshot());
});
// later: unsubscribe();
```

### Connection state

```ts
const client = new StreamClient({ url: "wss://your-gateway.example/ws" });

const off = client.subscribeState((state) => {
  console.log(state); // "disconnected" | "connecting" | "connected" | "reconnecting"
});

await client.connect();
// …
off();
```

### Custom WebSocket (Node / tests)

```ts
import { StreamClient } from "@jaewu/client";
import WebSocket from "ws";

const client = new StreamClient({
  url: "ws://localhost:4000/ws",
  createWebSocket: (url) => new WebSocket(url) as unknown as WebSocket,
});
```

### React: provider and hooks

```tsx
import { StreamProvider, useTopicPrice, useConnectionState } from "@jaewu/client/react";

function App() {
  return (
    <StreamProvider options={{ url: "wss://your-gateway.example/ws" }}>
      <Ticker topic="stock.AAPL" />
    </StreamProvider>
  );
}

function Ticker({ topic }: { topic: string }) {
  const price = useTopicPrice(topic);
  const state = useConnectionState();
  if (!price) return <span>{state} — no data yet</span>;
  return (
    <span>
      {price.topic} @ {price.value}
    </span>
  );
}
```

Reuse one client everywhere by passing it in: `<StreamProvider client={client} autoConnect />`. Use `autoConnect={false}` if you call `client.connect()` yourself (for example after auth).