ServerSentEvent Zig Reference Documentation
ServerSentEvent
Current Version: 11.6.1
Chilkat.ServerSentEvent
Use
Inspect
Use
Read
Use
Check 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
LoadEvent to parse a complete multi-line
Server-Sent Event block.
Read event name
EventName to determine the event type supplied by
the server, if an event: field is present.
Read event data
Data to obtain the event payload, such as JSON text from
a streaming API or real-time service.
Track event IDs
LastEventId when the event includes an
id: field that identifies the event or stream position.
Handle retry hints
Retry to read the server-provided retry interval when a
retry: field is included.
Diagnostics
LastErrorText if event text cannot be parsed or if a
parsing result is unexpected.
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.
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();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.
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.
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
pub fn getData(self: ServerSentEvent, allocator: Allocator) Allocator.Error![:0]u8
The data for the server-side event. (If the data field was empty, then this will be empty.)
DebugLogFilePath
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.
EventName
pub fn getEventName(self: ServerSentEvent, allocator: Allocator) Allocator.Error![:0]u8
The name of the server-side event. (If the event field was not present, then this will be empty.)
LastErrorHtml
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.
topLastErrorText
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.
LastErrorXml
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.
topLastEventId
pub fn getLastEventId(self: ServerSentEvent, allocator: Allocator) Allocator.Error![:0]u8
The content of the id field, if present.
LastMethodSuccess
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.
Retry
pub fn getRetry(self: ServerSentEvent) i32
The integer value of the retry field, if it exists; otherwise, 0.
VerboseLogging
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.
Version
pub fn getVersion(self: ServerSentEvent, allocator: Allocator) Allocator.Error![:0]u8
Methods
LoadEvent
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.