Skip to content

TypeScript/JavaScript SDK

The TypeScript/JavaScript SDK provides promise-based transport layers for Node.js and browser environments.

Installation

Generate with SDK:

Terminal window
struct-frame messages.sf --build-ts --ts-path src/generated/ --sdk

For JavaScript use --build-js --js-path ... --sdk; the API is identical (CommonJS require).

Basic Usage

import { StructFrameSdk, TcpTransport } from './generated/ts/struct-frame-sdk';
import { ProfileStandardConfig } from './generated/ts/frame-profiles';
import { Status, getMessageInfo } from './generated/ts/status.structframe';
const sdk = new StructFrameSdk({
transport: new TcpTransport({ host: '192.168.1.100', port: 8080 }),
profile: ProfileStandardConfig,
getMessageInfo,
onError: (error) => console.error('SDK error:', error),
});
// Register a codec so handlers receive decoded messages. Without one they receive the
// raw payload bytes (Uint8Array).
sdk.registerCodec({
getMsgId: () => Status.MSG_ID,
deserialize: (data) => Status.deserialize(data),
});
// Subscribe to messages by ID; subscribe() returns an unsubscribe function
const unsubscribe = sdk.subscribe<Status>(Status.MSG_ID, (msg) => {
console.log(`Status: ${msg.value}`);
});
await sdk.connect();
// Send a message, or send a request and await the matching reply
await sdk.send(new Status({ value: 1 }));
// const reply = await sdk.request<Pong>(ping, Pong.MSG_ID, { timeout: 2 });

onError receives transport errors, exceptions thrown by handlers and deserialization failures; without it they are only logged when debug: true.

Transports

UDP

import { UdpTransport } from './generated/ts/struct-frame-sdk';
const transport = new UdpTransport({ remoteHost: '192.168.1.100', remotePort: 8080 });
await transport.connect();

TCP

import { TcpTransport } from './generated/ts/struct-frame-sdk';
const transport = new TcpTransport({ host: '192.168.1.100', port: 8080 });
await transport.connect();

WebSocket

import { WebSocketTransport } from './generated/ts/struct-frame-sdk';
const transport = new WebSocketTransport({ url: 'ws://localhost:8080' });
await transport.connect();

Serial (Node.js only)

import { SerialTransport } from './generated/ts/struct-frame-sdk';
const transport = new SerialTransport({ path: '/dev/ttyUSB0', baudRate: 115200 });
await transport.connect();

Browser vs Node.js

The SDK works in both environments:

  • Node.js: Full support including Serial transport
  • Browser: WebSocket and (with polyfills) UDP/TCP
Node.js
// Detect environment
if (typeof window === 'undefined') {
const { SerialTransport } = await import('./struct-frame-sdk');
} else {
// Browser
const { WebSocketTransport } = await import('./struct-frame-sdk');
}