ServerSentEvent Zig Reference Documentation

ServerSentEvent

Current Version: 11.6.1

Chilkat.ServerSentEvent

Parse server-sent event text into event name, data, ID, and retry fields.

Chilkat.ServerSentEvent is a small helper class for parsing Server-Sent Events formatted text. Given a multi-line event block, it extracts the event name, data payload, id value, and retry interval into simple read-only properties. It is useful when an application receives event-stream text and needs to work with the structured event fields instead of manually parsing the raw lines.

Load event text

Use LoadEvent to parse a complete multi-line Server-Sent Event block.

Read event name

Inspect EventName to determine the event type supplied by the server, if an event: field is present.

Read event data

Use Data to obtain the event payload, such as JSON text from a streaming API or real-time service.

Track event IDs

Read LastEventId when the event includes an id: field that identifies the event or stream position.

Handle retry hints

Use Retry to read the server-provided retry interval when a retry: field is included.

Diagnostics

Check LastErrorText if event text cannot be parsed or if a parsing result is unexpected.

Relationship to Chilkat.WebSocket: ServerSentEvent does not manage a WebSocket connection or parse WebSocket frames. Instead, it can be used after Chilkat.WebSocket receives a text message whose payload is formatted as a Server-Sent Event. Read the WebSocket text frame, pass the event text to LoadEvent, then use EventName, Data, LastEventId, and Retry to process the event.
Common pattern: Receive or otherwise obtain a complete Server-Sent Event text block, call LoadEvent, then inspect EventName to route the event and Data to process the payload. Use LastEventId and Retry when the stream provides reconnection or event-position information.

Object Creation

// Add the package once:
//     zig fetch --save https://chilkatdownload.com/11.6.1/chilkat-zig-11.6.1.tar.gz
// and in build.zig:
//     const chilkat = b.dependency("chilkat", .{ .target = target, .optimize = optimize });
//     exe.root_module.addImport("chilkat", chilkat.module("chilkat"));
const chilkat = @import("chilkat");

// Once per process, before any other Chilkat call:
try chilkat.unlockBundle("Anything for 30-day trial");

const server_sent_event = try chilkat.ServerSentEvent.init();
defer server_sent_event.deinit();
pub fn init() Allocator.Error!ServerSentEvent

Creates the underlying native Chilkat object. ServerSentEvent is a one-pointer struct passed by value; copying it copies the handle (two names for one object). Returns error.OutOfMemory if the library could not allocate the object. Use an object from one thread at a time; it may be handed from one thread to another.

pub fn deinit(self: ServerSentEvent) void

Releases the native object. Call it exactly once per object (usually with defer); the handle is invalid afterwards. Objects returned by methods are owned by the caller too and are released the same way.

pub fn fromHandle(h: *chilkat.c.CkServerSentEvent.HCkServerSentEvent) ServerSentEvent

Wraps a handle obtained from the C API (chilkat.c.CkServerSentEvent), taking ownership of it. The struct's handle field goes the other way, for anything the Zig API does not cover.

Errors and memory

Methods that can fail return an error union: a method whose only outcome is success or failure returns Error!void; a method producing a string returns (Error || Allocator.Error)![:0]u8; a method producing an object returns Error!T. chilkat.Error is error{ChilkatFailed}; the reason for a failure is in getLastErrorText, and the object remains usable. Properties never fail, and methods that answer a question (hasMember, isUnlocked, ...) return a plain bool.

String arguments are [:0]const u8 (UTF-8; string literals can be passed as is). String results are copies allocated with the allocator argument and owned by the caller, so they stay valid across later calls on the same object.

const text = server_sent_event.someMethod(allocator, ...) catch |err| {
    const why = try server_sent_event.getLastErrorText(allocator);
    defer allocator.free(why);
    std.debug.print("{s}\n", .{why});
    return err;
};
defer allocator.free(text);

Properties

Data
// read-only
pub fn getData(self: ServerSentEvent, allocator: Allocator) Allocator.Error![:0]u8
Introduced in version 9.5.0.58

The data for the server-side event. (If the data field was empty, then this will be empty.)

top
DebugLogFilePath
// read/write
pub fn getDebugLogFilePath(self: ServerSentEvent, allocator: Allocator) Allocator.Error![:0]u8
pub fn setDebugLogFilePath(self: ServerSentEvent, value: [:0]const u8) void

If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.

Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.

Possible causes of hangs include:

  • A timeout property set to 0, indicating an infinite timeout.
  • A hang occurring within an event callback in the application code.
  • An internal bug in the Chilkat code causing the hang.

More Information and Examples
top
EventName
// read-only
pub fn getEventName(self: ServerSentEvent, allocator: Allocator) Allocator.Error![:0]u8
Introduced in version 9.5.0.58

The name of the server-side event. (If the event field was not present, then this will be empty.)

top
LastErrorHtml
// read-only
pub fn getLastErrorHtml(self: ServerSentEvent, allocator: Allocator) Allocator.Error![:0]u8

Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorText
// read-only
pub fn getLastErrorText(self: ServerSentEvent, allocator: Allocator) Allocator.Error![:0]u8

Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorXml
// read-only
pub fn getLastErrorXml(self: ServerSentEvent, allocator: Allocator) Allocator.Error![:0]u8

Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastEventId
// read-only
pub fn getLastEventId(self: ServerSentEvent, allocator: Allocator) Allocator.Error![:0]u8
Introduced in version 9.5.0.58

The content of the id field, if present.

top
LastMethodSuccess
// read/write
pub fn getLastMethodSuccess(self: ServerSentEvent) bool
pub fn setLastMethodSuccess(self: ServerSentEvent, value: bool) void

Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.

top
Retry
// read-only
pub fn getRetry(self: ServerSentEvent) i32
Introduced in version 9.5.0.58

The integer value of the retry field, if it exists; otherwise, 0.

top
VerboseLogging
// read/write
pub fn getVerboseLogging(self: ServerSentEvent) bool
pub fn setVerboseLogging(self: ServerSentEvent, value: bool) void

If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
// read-only
pub fn getVersion(self: ServerSentEvent, allocator: Allocator) Allocator.Error![:0]u8

Version of the component/library, such as "10.1.0"

More Information and Examples
top

Methods

LoadEvent
pub fn loadEvent(self: ServerSentEvent, event_text: [:0]const u8) Error!void
Introduced in version 9.5.0.58

Loads the multi-line event text into this object. For example, the event_text for a Firebase event might look like this:

event: put
data: {"path": "/c", "data": {"foo": true, "bar": false}}

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top