Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions ably/example_message_updates_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package ably_test

import (
"context"
"fmt"

"github.com/ably/ably-go/ably"
)

// Example demonstrating how to publish a message and get its serial
func ExampleRESTChannel_PublishWithResult() {
client, err := ably.NewREST(ably.WithKey("xxx:xxx"))
if err != nil {
panic(err)
}

channel := client.Channels.Get("example-channel")

// Publish a message and get its serial
result, err := channel.PublishWithResult(context.Background(), "event-name", "message data")
if err != nil {
panic(err)
}

if result.Serial == nil {
fmt.Println("Message published but serial not available (discarded by conflation)")
return
}
fmt.Printf("Message published with serial: %s\n", *result.Serial)
}
Comment thread
lmars marked this conversation as resolved.

// Example demonstrating how to update a message
func ExampleRESTChannel_UpdateMessage() {
client, err := ably.NewREST(ably.WithKey("xxx:xxx"))
if err != nil {
panic(err)
}

channel := client.Channels.Get("example-channel")

// First publish a message to get its serial
result, err := channel.PublishWithResult(context.Background(), "event", "initial data")
if err != nil {
panic(err)
}

if result.Serial == nil {
fmt.Println("Message published but serial not available (discarded by conflation)")
return
}

// Update the message
msg := &ably.Message{
Serial: *result.Serial,
Data: "updated data",
}

updateResult, err := channel.UpdateMessage(
context.Background(),
msg,
ably.UpdateWithDescription("Fixed typo"),
ably.UpdateWithMetadata(map[string]string{"editor": "alice"}),
)
if err != nil {
panic(err)
}

Comment thread
lmars marked this conversation as resolved.
if updateResult.VersionSerial == nil {
fmt.Println("Message updated but version serial not available (superseded)")
return
}
fmt.Printf("Message updated with version serial: %s\n", *updateResult.VersionSerial)
}

// Example demonstrating async message append for AI streaming
func ExampleRealtimeChannel_AppendMessageAsync() {
client, err := ably.NewRealtime(ably.WithKey("xxx:xxx"))
if err != nil {
panic(err)
}

channel := client.Channels.Get("chat-channel")

// Publish initial message
result, err := channel.PublishWithResult(context.Background(), "ai-response", "The answer is")
if err != nil {
panic(err)
}

if result.Serial == nil {
fmt.Println("Message published but serial not available (discarded by conflation)")
return
}

// Stream tokens asynchronously without blocking
tokens := []string{" 42", ".", " This", " is", " the", " answer."}
for _, token := range tokens {
msg := &ably.Message{
Serial: *result.Serial,
Data: token,
}
// Non-blocking append - critical for AI streaming
err := channel.AppendMessageAsync(msg, func(r *ably.UpdateDeleteResult, err error) {
if err != nil {
fmt.Printf("Append failed: %v\n", err)
}
})
if err != nil {
panic(err)
}
}

fmt.Println("All tokens queued for append")
}
2 changes: 1 addition & 1 deletion ably/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (c *Connection) AckAll() {
c.mtx.Unlock()
c.log().Infof("Ack all %d messages waiting for ACK/NACK", len(cx))
for _, v := range cx {
v.onAck(nil)
v.ackCallback.call(nil, nil)
}
}

Expand Down
Loading
Loading