Skip to content

Commit 4dfcb74

Browse files
authored
Merge pull request #3351 from ably/go-update-delete-append-examples
Add Go examples for message updates, deletes, and appends
2 parents 4ca19a3 + 9d8b1d0 commit 4dfcb74

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

src/pages/docs/messages/updates-deletes.mdx

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,31 @@ operation = MessageOperation(description="reason for update")
215215
result = await channel.update_message(message, operation)
216216
print("Message updated")
217217
```
218+
219+
```go
220+
realtime, err := ably.NewRealtime(ably.WithKey("{{API_KEY}}"))
221+
if err != nil {
222+
panic(err)
223+
}
224+
// This assumes there is an 'updates' namespace with a rule enabling updates and deletes
225+
channel := realtime.Channels.Get("updates:example")
226+
227+
// Publish the original message and get its serial from the result
228+
result, err := channel.PublishWithResult(context.Background(), "message-name", "original-data")
229+
if err != nil {
230+
panic(err)
231+
}
232+
233+
// Publish an update using the serial
234+
msg := &ably.Message{
235+
Serial: *result.Serial,
236+
Data: "updated-data",
237+
}
238+
_, err = channel.UpdateMessage(context.Background(), msg, ably.UpdateWithDescription("reason for update"))
239+
if err != nil {
240+
panic(err)
241+
}
242+
```
218243
</Code>
219244

220245
#### Returns
@@ -399,6 +424,31 @@ operation = MessageOperation(description="reason for delete")
399424
result = await channel.delete_message(message, operation)
400425
print("Message deleted")
401426
```
427+
428+
```go
429+
realtime, err := ably.NewRealtime(ably.WithKey("{{API_KEY}}"))
430+
if err != nil {
431+
panic(err)
432+
}
433+
// This assumes there is an 'updates' namespace with a rule enabling updates and deletes
434+
channel := realtime.Channels.Get("updates:example")
435+
436+
// Publish the original message and get its serial from the result
437+
result, err := channel.PublishWithResult(context.Background(), "message-name", "original-data")
438+
if err != nil {
439+
panic(err)
440+
}
441+
442+
// Delete the message using the serial
443+
msg := &ably.Message{
444+
Serial: *result.Serial,
445+
Data: "", // clear the previous data
446+
}
447+
_, err = channel.DeleteMessage(context.Background(), msg, ably.UpdateWithDescription("reason for delete"))
448+
if err != nil {
449+
panic(err)
450+
}
451+
```
402452
</Code>
403453

404454
#### Returns
@@ -608,6 +658,35 @@ channel.publish([.init(name: "message-name", data: "Hello")]) { result, error in
608658
}
609659
}
610660
```
661+
662+
```go
663+
realtime, err := ably.NewRealtime(ably.WithKey("{{API_KEY}}"))
664+
if err != nil {
665+
panic(err)
666+
}
667+
// This assumes there is an 'updates' namespace with a rule enabling updates and deletes
668+
channel := realtime.Channels.Get("updates:example")
669+
670+
// Publish the original message and get its serial from the result
671+
result, err := channel.PublishWithResult(context.Background(), "message-name", "Hello")
672+
if err != nil {
673+
panic(err)
674+
}
675+
676+
// Append to the message a few times (without needing to await each to finish
677+
// before doing the next); the data will be concatenated
678+
for _, fragment := range []string{", ", "World", "!"} {
679+
err := channel.AppendMessageAsync(&ably.Message{
680+
Serial: *result.Serial,
681+
Data: fragment,
682+
}, nil)
683+
if err != nil {
684+
panic(err)
685+
}
686+
}
687+
688+
// the message in history now has data: "Hello, World!"
689+
```
611690
</Code>
612691

613692
#### Returns
@@ -734,6 +813,23 @@ serial = "0123456789-001@abcdefghij:001"
734813

735814
message = await channel.get_message(serial)
736815
```
816+
817+
```go
818+
rest, err := ably.NewREST(ably.WithKey("{{API_KEY}}"))
819+
if err != nil {
820+
panic(err)
821+
}
822+
channel := rest.Channels.Get("updates:example")
823+
824+
// Example serial; for example from the `serial` property of a `Message` you previously received
825+
serial := "0123456789-001@abcdefghij:001"
826+
827+
message, err := channel.GetMessage(context.Background(), serial)
828+
if err != nil {
829+
panic(err)
830+
}
831+
_ = message
832+
```
737833
</Code>
738834

739835
## Get message versions <a id="versions"/>
@@ -800,6 +896,32 @@ serial = "0123456789-001@abcdefghij:001"
800896
page = await channel.getMessageVersions(serial)
801897
print("Found " + len(page.items) + " versions");
802898
```
899+
900+
```go
901+
rest, err := ably.NewREST(ably.WithKey("{{API_KEY}}"))
902+
if err != nil {
903+
panic(err)
904+
}
905+
channel := rest.Channels.Get("updates:example")
906+
907+
// Example serial; for example from the `serial` property of a `Message` you previously received
908+
serial := "0123456789-001@abcdefghij:001"
909+
910+
pages, err := channel.GetMessageVersions(serial, nil).Pages(context.Background())
911+
if err != nil {
912+
panic(err)
913+
}
914+
for pages.Next(context.Background()) {
915+
versions := pages.Items()
916+
fmt.Printf("Found %d versions on this page\n", len(versions))
917+
for _, msg := range versions {
918+
fmt.Printf(" %s: %v\n", msg.Serial, msg.Data)
919+
}
920+
}
921+
if err := pages.Err(); err != nil {
922+
panic(err)
923+
}
924+
```
803925
</Code>
804926

805927
## Message version structure <a id="version-structure"/>

0 commit comments

Comments
 (0)