-
Notifications
You must be signed in to change notification settings - Fork 46
Add Go examples for message updates, deletes, and appends #3351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lmars
wants to merge
1
commit into
main
Choose a base branch
from
go-update-delete-append-examples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -215,6 +215,31 @@ operation = MessageOperation(description="reason for update") | |
| result = await channel.update_message(message, operation) | ||
| print("Message updated") | ||
| ``` | ||
|
|
||
| ```go | ||
| realtime, err := ably.NewRealtime(ably.WithKey("{{API_KEY}}")) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| // This assumes there is an 'updates' namespace with a rule enabling updates and deletes | ||
| channel := realtime.Channels.Get("updates:example") | ||
|
|
||
| // Publish the original message and get its serial from the result | ||
| result, err := channel.PublishWithResult(context.Background(), "message-name", "original-data") | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| // Publish an update using the serial | ||
| msg := &ably.Message{ | ||
| Serial: *result.Serial, | ||
| Data: "updated-data", | ||
| } | ||
| _, err = channel.UpdateMessage(context.Background(), msg, ably.UpdateWithDescription("reason for update")) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| ``` | ||
| </Code> | ||
|
|
||
| #### Returns | ||
|
|
@@ -399,6 +424,31 @@ operation = MessageOperation(description="reason for delete") | |
| result = await channel.delete_message(message, operation) | ||
| print("Message deleted") | ||
| ``` | ||
|
|
||
| ```go | ||
| realtime, err := ably.NewRealtime(ably.WithKey("{{API_KEY}}")) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| // This assumes there is an 'updates' namespace with a rule enabling updates and deletes | ||
| channel := realtime.Channels.Get("updates:example") | ||
|
|
||
| // Publish the original message and get its serial from the result | ||
| result, err := channel.PublishWithResult(context.Background(), "message-name", "original-data") | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| // Delete the message using the serial | ||
| msg := &ably.Message{ | ||
| Serial: *result.Serial, | ||
| Data: "", // clear the previous data | ||
| } | ||
| _, err = channel.DeleteMessage(context.Background(), msg, ably.UpdateWithDescription("reason for delete")) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| ``` | ||
| </Code> | ||
|
|
||
| #### Returns | ||
|
|
@@ -608,6 +658,35 @@ channel.publish([.init(name: "message-name", data: "Hello")]) { result, error in | |
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```go | ||
| realtime, err := ably.NewRealtime(ably.WithKey("{{API_KEY}}")) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| // This assumes there is an 'updates' namespace with a rule enabling updates and deletes | ||
| channel := realtime.Channels.Get("updates:example") | ||
|
|
||
| // Publish the original message and get its serial from the result | ||
| result, err := channel.PublishWithResult(context.Background(), "message-name", "Hello") | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| // Append to the message a few times (without needing to await each to finish | ||
| // before doing the next); the data will be concatenated | ||
| for _, fragment := range []string{", ", "World", "!"} { | ||
| err := channel.AppendMessageAsync(&ably.Message{ | ||
| Serial: *result.Serial, | ||
| Data: fragment, | ||
| }, nil) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| } | ||
|
|
||
| // the message in history now has data: "Hello, World!" | ||
| ``` | ||
| </Code> | ||
|
|
||
| #### Returns | ||
|
|
@@ -734,6 +813,23 @@ serial = "0123456789-001@abcdefghij:001" | |
|
|
||
| message = await channel.get_message(serial) | ||
| ``` | ||
|
|
||
| ```go | ||
| rest, err := ably.NewREST(ably.WithKey("{{API_KEY}}")) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| channel := rest.Channels.Get("updates:example") | ||
|
|
||
| // Example serial; for example from the `serial` property of a `Message` you previously received | ||
| serial := "0123456789-001@abcdefghij:001" | ||
|
|
||
| message, err := channel.GetMessage(context.Background(), serial) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| _ = message | ||
| ``` | ||
| </Code> | ||
|
|
||
| ## Get message versions <a id="versions"/> | ||
|
|
@@ -800,6 +896,24 @@ serial = "0123456789-001@abcdefghij:001" | |
| page = await channel.getMessageVersions(serial) | ||
| print("Found " + len(page.items) + " versions"); | ||
| ``` | ||
|
|
||
| ```go | ||
| rest, err := ably.NewREST(ably.WithKey("{{API_KEY}}")) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| channel := rest.Channels.Get("updates:example") | ||
|
|
||
| // Example serial; for example from the `serial` property of a `Message` you previously received | ||
| serial := "0123456789-001@abcdefghij:001" | ||
|
|
||
| pages := channel.GetMessageVersions(serial, nil) | ||
| page, err := pages.Next(context.Background()) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| fmt.Printf("Found %d versions\n", len(page.Items)) | ||
| ``` | ||
|
Comment on lines
+910
to
+916
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to use the version below because pages, err := channel.GetMessageVersions(serial, nil).Pages(context.Background())
if err != nil {
panic(err)
}
for pages.Next(context.Background()) {
items := pages.Items()
fmt.Printf("Found %d versions on this page\n", len(items))
for _, msg := range items {
fmt.Printf(" %s: %v\n", msg.Serial, msg.Data)
}
}
if err := pages.Err(); err != nil {
panic(err)
} |
||
| </Code> | ||
|
|
||
| ## Message version structure <a id="version-structure"/> | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My Go admittedly isn't the best, but this fails when I test it - according to Claude because: