@net-services/client
v0.1.0
Published
Node + browser SDK: connect to the gateway and send requests to a service over WebRTC
Maintainers
Readme
@net-services/client
Front-line SDK (Node + browser) for net-services: connect to the gateway,
negotiate a peer-to-peer WebRTC DataChannel with the matched service-server,
then send requests and exchange pub/sub events — all peer-to-peer, encoded with
datapack.
The gateway only authenticates and relays WebRTC signalling. The server is the WebRTC offerer; this client answers and receives the channel.
Install
npm install @net-services/clientIn the browser, the global WebSocket and RTCPeerConnection are used — no
extra dependencies. In Node, install the optional peers:
npm install @net-services/client node-rtc-connection wsUsage
import { ServiceClient } from "@net-services/client";
import { pack, unpack, STRING } from "datapack";
const client = new ServiceClient({
gatewayUrl: "wss://gateway.example.com/ws",
serviceId: "svc_...", // from the admin console
});
await client.connect();
// Request/reply — payloads are opaque bytes; pick your own datapack schema.
const res = await client.request("echo", pack({ text: "hi" }, { text: STRING }));
console.log(res.status, unpack(res.data, { text: STRING }).text);
// Pub/sub:
client.subscribe("chat", (payload, event) => {
console.log(event.from, unpack(payload, { text: STRING }).text);
});
client.publish("chat", pack({ text: "hello" }, { text: STRING }));
// Broadcast and topic events arrive on the 'event' listener too:
client.on("event", (payload, event) => { /* ... */ });API
new ServiceClient(options)—{ gatewayUrl, serviceId, serverId?, requestTimeoutMs?, debug? }..connect()/.close()— open / close the gateway connection..request(type, payload)— round-trip a request; resolves to{ status, data }..subscribe(topic, cb?)/.unsubscribe(topic, cb?)— topic pub/sub..publish(topic, payload)— publish bytes to a topic..on(event, cb)— lifecycle events:open,event,peergone,error,close.
Payloads (request, publish) and event payloads are opaque Uint8Arrays —
the application chooses its own datapack schema for each.
License
MIT
