diff --git a/src/client.zig b/src/client.zig index e54998b..1217124 100644 --- a/src/client.zig +++ b/src/client.zig @@ -481,3 +481,223 @@ pub const Client = struct { return try protocol.decodePubSubMessage(allocator, frame.payload); } + /// Remote `publish `: delivers `message` to every + /// subscriber currently registered on `channel` across every connection + /// the server has, returning how many subscribers it reached (0 is a + /// normal outcome for a channel nobody is listening to, not an error - + /// see `pubsub.Hub.publish`'s doc comment). Any connection may publish, + /// whether or not it has ever subscribed to anything itself. + pub fn publish(self: *Client, channel: []const u8, message: []const u8) !u64 { + const payload = try protocol.encodePublish(self.allocator, channel, message); + defer self.allocator.free(payload); + var frame = try self.roundTrip(.publish, payload); + defer frame.deinit(); + const status = try protocol.Status.fromByte(frame.tag); + if (status != .ok) return statusToError(status); + return protocol.decodePublishResult(frame.payload); + } + + /// Remote `txn