ably-go

module
v1.2.0-apipreview.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2020 License: Apache-2.0

README

Ably Go PkgGoDev

A Go client library for www.ably.io, the realtime messaging service.

Installation

~ $ go get -u github.com/ably/ably-go/ably

Using the Realtime API

Creating a client

client, err := ably.NewRealtime(ably.WithKey("xxx:xxx"))
if err != nil {
	panic(err)
}

channel := client.Channels.Get("test")

Subscribing to events

You may monitor events on connections and channels.

client, err = ably.NewRealtime(
	ably.WithKey("xxx:xxx"),
	ably.WithAutoConnect(false), // Set this option to avoid missing state changes.
)
if err != nil {
	panic(err)
}

// Set up connection events handler.
client.Connection.OnAll(func(change ably.ConnectionStateChange) {
	fmt.Printf("Connection event: %s state=%s reason=%s", change.Event, change.Current, change.Reason)
})

// Then connect.
client.Connect()

channel = client.Channels.Get("test")

channel.OnAll(func(change ably.ChannelStateChange) {
	fmt.Printf("Channel event event: %s channel=%s state=%s reason=%s", channel.Name, change.Event, change.Current, change.Reason)
})

Subscribing to a channel for all messages

unsubscribe, err := channel.SubscribeAll(ctx, func(msg *ably.Message) {
	fmt.Printf("Received message: name=%s data=%v\n", msg.Name, msg.Data)
})
if err != nil {
	panic(err)
}

Subscribing to a channel for EventName1 and EventName2 message names

unsubscribe1, err := channel.Subscribe(ctx, "EventName1", func(msg *ably.Message) {
	fmt.Printf("Received message: name=%s data=%v\n", msg.Name, msg.Data)
})
if err != nil {
	panic(err)
}

unsubscribe2, err := channel.Subscribe(ctx, "EventName2", func(msg *ably.Message) {
	fmt.Printf("Received message: name=%s data=%v\n", msg.Name, msg.Data)
})
if err != nil {
	panic(err)
}

Publishing to a channel

err = channel.Publish(ctx, "EventName1", "EventData1")
if err != nil {
	panic(err)
}

Handling errors

Errors returned by this library may have an underlying *ErrorInfo type.

See Ably documentation for ErrorInfo.

badClient, err := ably.NewRealtime(ably.WithKey("invalid:key"))
if err != nil {
	panic(err)
}

err = badClient.Channels.Get("test").Publish(ctx, "event", "data")
if errInfo := (*ably.ErrorInfo)(nil); errors.As(err, &errInfo) {
	fmt.Printf("Error publishing message: code=%v status=%v cause=%v", errInfo.Code, errInfo.StatusCode, errInfo.Cause)
} else if err != nil {
	panic(err)
}

Announcing presence on a channel

err = channel.Presence.Enter(ctx, "presence data")
if err != nil {
	panic(err)
}

Announcing presence on a channel on behalf of other client

err = channel.Presence.EnterClient(ctx, "clientID", "presence data")
if err != nil {
	panic(err)
}

Updating and leaving presence

// Update also has an UpdateClient variant.
err = channel.Presence.Update(ctx, "new presence data")
if err != nil {
	panic(err)
}

// Leave also has an LeaveClient variant.
err = channel.Presence.Leave(ctx, "last presence data")
if err != nil {
	panic(err)
}

Getting all clients present on a channel

clients, err := channel.Presence.Get(ctx)
if err != nil {
	panic(err)
}

for _, client := range clients {
	fmt.Println("Present client:", client)
}

Subscribing to all presence messages

unsubscribe, err = channel.Presence.SubscribeAll(ctx, func(msg *ably.PresenceMessage) {
	fmt.Printf("Presence event: action=%v data=%v", msg.Action, msg.Data)
})
if err != nil {
	panic(err)
}

Subscribing to 'Enter' presence messages only

unsubscribe, err = channel.Presence.Subscribe(ctx, ably.PresenceActionEnter, func(msg *ably.PresenceMessage) {
	fmt.Printf("Presence event: action=%v data=%v", msg.Action, msg.Data)
})
if err != nil {
	panic(err)
}

Using the REST API

Introduction

All examples assume a client and/or channel has been created as follows:

client, err := ably.NewREST(ably.WithKey("xxx:xxx"))
if err != nil {
	panic(err)
}

channel := client.Channels.Get("test")

Publishing a message to a channel

err = channel.Publish(ctx, "HelloEvent", "Hello!")
if err != nil {
	panic(err)
}

// You can also publish a batch of messages in a single request.
err = channel.PublishBatch(ctx, []*ably.Message{
	{Name: "HelloEvent", Data: "Hello!"},
	{Name: "ByeEvent", Data: "Bye!"},
})
if err != nil {
	panic(err)
}

Querying the History

page, err := channel.History(nil)
for ; err == nil && page != nil; page, err = page.Next() {
	for _, message := range page.Messages() {
		fmt.Println(message)
	}
}
if err != nil {
	panic(err)
}

Presence on a channel

page, err = channel.Presence.Get(nil)
for ; err == nil && page != nil; page, err = page.Next() {
	for _, presence := range page.PresenceMessages() {
		fmt.Println(presence)
	}
}
if err != nil {
	panic(err)
}

Querying the Presence History

page, err = channel.Presence.History(nil)
for ; err == nil && page != nil; page, err = page.Next() {
	for _, presence := range page.PresenceMessages() {
		fmt.Println(presence)
	}
}
if err != nil {
	panic(err)
}

Fetching your application's stats

page, err = client.Stats(&ably.PaginateParams{})
for ; err == nil && page != nil; page, err = page.Next() {
	for _, stat := range page.Stats() {
		fmt.Println(stat)
	}
}
if err != nil {
	panic(err)
}

Feature support

This library targets the Ably 1.2 client library specification.

Known limitations

As of release 1.2.0, the following are not implemented and will be covered in future 1.2.x releases. If there are features that are currently missing that are a high priority for your use-case then please contact Ably customer support. Pull Requests are also welcomed.

REST API

Realtime API

  • There is no channel suspended state; this means that the client will not automatically reattach to channels if a connection becomes suspended and then resumes, and presence members associated with the client will not be automatically re-entered.

  • Transient realtime publishing is not supported, so a call to publish() on a realtime channel will trigger attachment of the channel.

  • Inband reauthentication is not supported; expiring tokens will trigger a disconnection and resume of a realtime connection.

  • Realtime connection failure handling is partially implemented.

  • Realtime Ping function is not implemented.

Release process

Starting with release 1.2, this library uses semantic versioning. For each release, the following needs to be done:

  • Create a branch for the release, named like release/1.2.0
  • Replace all references of the current version number with the new version number and commit the changes
  • Run github_changelog_generator to automate the update of the CHANGELOG. This may require some manual intervention, both in terms of how the command is run and how the change log file is modified. Your mileage may vary:
    • The command you will need to run will look something like this: github_changelog_generator -u ably -p ably-go --since-tag v1.1.4 --output delta.md
    • Using the command above, --output delta.md writes changes made after --since-tag to a new file
    • The contents of that new file (delta.md) then need to be manually inserted at the top of the CHANGELOG.md, changing the "Unreleased" heading and linking with the current version numbers
    • Also ensure that the "Full Changelog" link points to the new version tag instead of the HEAD
    • Commit this change: git add CHANGELOG.md && git commit -m "Update change log."
  • Commit CHANGELOG
  • Make a PR against main
  • Once the PR is approved, merge it into main
  • Add a tag to the new main head commit and push to origin such as git tag v1.2.0 && git push origin v1.2.0

Support and feedback

Please visit http://support.ably.io/ for access to our knowledgebase and to ask for any assistance.

You can also view the community reported Github issues.

Contributing

Because this package uses internal packages, all fork development has to happen under $GOPATH/src/github.com/ably/ably-go to prevent use of internal package not allowed errors.

  1. Fork github.com/ably/ably-go
  2. go to the ably-go directory: cd $GOPATH/src/github.com/ably/ably-go
  3. add your fork as a remote: git remote add fork git@github.com:your-username/ably-go
  4. create your feature branch: git checkout -b my-new-feature
  5. commit your changes (git commit -am 'Add some feature')
  6. ensure you have added suitable tests and the test suite is passing: make test
  7. push to the branch: git push fork my-new-feature
  8. create a new Pull Request

Directories

Path Synopsis
Package ably is the official Ably client library for Go.
Package ably is the official Ably client library for Go.
scripts
test_readme_examples
Command test_readme_examples, when run at the /ably directory, generates a readme_examples_test.go file with the examples from README.md.
Command test_readme_examples, when run at the /ably directory, generates a readme_examples_test.go file with the examples from README.md.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL