A publication allows you to send messages to an Aeron stream.
- Create the publication: Call
a.AddPublication(channel, streamId). This returns a channel that yields the publication once it is registered with the driver. - Prepare the buffer: Use
buffers.MakeAtomic([]byte) to wrap your message payload. - Wait for connection (Optional): You can poll
publication.IsConnected() to ensure subscribers are ready before sending. - Offer the message: Call
publication.Offer(srcBuffer, offset, length, options).
Offer Return Codes:
aeron.NotConnected: The publication is not yet connected to any subscribers.aeron.BackPressured: The publication is being throttled due to congestion.ret >= 0: Success (the specific value indicates the number of fragments written).ret < 0: An unrecognized error code.
publication := <-a.AddPublication("aeron:ipc", 10)
defer publication.Close()
// Prepare message
message := "this is a message"
srcBuffer := buffers.MakeAtomic(([]byte)(message))
// Wait for connection
for !publication.IsConnected() {
time.Sleep(time.Millisecond * 10)
}
// Send message
ret := publication.Offer(srcBuffer, 0, int32(len(message)), nil)
switch ret {
case aeron.NotConnected:
log.Print("not connected yet")
case aeron.BackPressured:
log.Print("back pressured")
default:
if ret < 0 {
log.Print("Unrecognized code: %d", ret)
} else {
log.Print("success!")
}
}
publication := <-a.AddPublication("aeron:ipc", 10)
defer publication.Close()
message := "this is a message"
srcBuffer := buffers.MakeAtomic(([]byte)(message))
for !publication.IsConnected() {
time.Sleep(time.Millisecond * 10)
}
ret := publication.Offer(srcBuffer, 0, int32(len(message)), nil)
switch ret {
case aeron.NotConnected:
log.Print("not connected yet")
case aeron.BackPressured:
log.Print("back pressured")
default:
if ret < 0 {
log.Print("Unrecognized code: %d", ret)
} else {
log.Print("success!")
}
}