Skip to main content

SlungClient

The main client class for connecting to Slung and sending events.

Constructor

new SlungClient(url?: string)
Create a new SlungClient instance.
url
string
default:"ws://127.0.0.1:2077"
WebSocket URL of the Slung server

Example

import { SlungClient } from "@slunghq/client";

const client = new SlungClient("ws://127.0.0.1:2077");

connect()

async connect(): Promise<void>
Establish a WebSocket connection to the Slung server. This method is idempotent - if already connected, it returns immediately.

Returns

Promise that resolves when the connection is established.

Throws

Throws an error if the connection fails.

Example

try {
  await client.connect();
  console.log("Connected to Slung");
} catch (err) {
  console.error("Connection failed:", err);
}

sendEvent()

sendEvent(event: SlungEvent): void
Send a single event to Slung. The event is encoded using the binary wire protocol and transmitted over the WebSocket connection.
event
SlungEvent
required
The event object to send
event.value
number
required
Numeric value associated with the event
event.timestamp
number
required
Unix timestamp in microseconds. Use Date.now() * 1000 or the nowUnixMicros() helper.
event.series
string
required
Series name identifying the metric or measurement
event.tags
string[]
required
Array of tags in key=value format

Throws

Throws an error if the WebSocket is not connected. Call connect() first.

Example

import { nowUnixMicros } from "@slunghq/client";

client.sendEvent({
  value: 23.5,
  timestamp: nowUnixMicros(),
  series: "temp",
  tags: ["sensor=1", "location=datacenter", "rack=A1"]
});

startSimulatedStream()

startSimulatedStream(options?: SimulatedStreamOptions): () => void
Start sending simulated events at regular intervals. Useful for testing, demos, and development.
options
SimulatedStreamOptions
Configuration options for the simulated stream
options.intervalMs
number
default:500
Milliseconds between events
options.initialValue
number
default:20
Starting numeric value
options.jitter
number
Random variance magnitude applied each interval
options.series
string
default:"temp"
Series name for generated events
options.tags
string[]
default:"[\"sensor=1\", \"env=dev\"]"
Tags array for generated events

Returns

A function that stops the simulated stream when called.

Example

const stop = client.startSimulatedStream({
  intervalMs: 10,
  initialValue: 90,
  jitter: 1.2,
  series: "cpu_usage",
  tags: ["host=server1", "env=prod"]
});

// Stop after 10 seconds
setTimeout(() => {
  stop();
  console.log("Stopped simulated stream");
}, 10000);

close()

close(code?: number, reason?: string): void
Close the WebSocket connection to Slung.
code
number
WebSocket close code (e.g., 1000 for normal closure)
reason
string
Human-readable reason for closing

Example

// Normal closure
client.close(1000, "shutdown");

// Simple close
client.close();

Utility Functions

nowUnixMicros()

function nowUnixMicros(): number
Returns the current Unix timestamp in microseconds.

Returns

Current time as microseconds since Unix epoch.

Example

import { nowUnixMicros } from "@slunghq/client";

const timestamp = nowUnixMicros();
console.log(timestamp); // e.g., 1709481234567890

encodeEventBinary()

function encodeEventBinary(event: WireEvent): Buffer
Encode an event to Slung’s binary wire format.
event
WireEvent
required
Event to encode

Returns

Buffer containing the encoded event data.

Example

import { encodeEventBinary } from "@slunghq/client";

const buffer = encodeEventBinary({
  timestamp: Date.now() * 1000,
  value: 42.0,
  series: "requests",
  tags: ["method=GET", "status=200"]
});

decodeEventBinary()

function decodeEventBinary(buf: Buffer): WireEvent | null
Decode a binary buffer to a WireEvent object.
buf
Buffer
required
Buffer containing encoded event data

Returns

Decoded WireEvent object, or null if the buffer is invalid.

createEventBinaryEncoder()

function createEventBinaryEncoder(
  series: string,
  tags: string[]
): (timestamp: number, value: number) => Buffer
Create a reusable encoder function for events with fixed series and tags.
series
string
required
Series name to encode in all events
tags
string[]
required
Tags array to encode in all events

Returns

Encoder function that takes (timestamp, value) and returns an encoded Buffer.

Example

import { createEventBinaryEncoder } from "@slunghq/client";

const encode = createEventBinaryEncoder("temp", ["sensor=1", "env=prod"]);

// Reuse the encoder for multiple events
const buf1 = encode(Date.now() * 1000, 23.5);
const buf2 = encode(Date.now() * 1000, 24.1);

Build docs developers (and LLMs) love