GoLang Client API

light. powerful. fast. easy.

The AMPS Client for Go is a lightweight client that combines performance, flexibility and ease-of-use in just the right way to make your messaging applications shine.

Example 1: Connect and Subscribe

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main

import (
    "amps"
    "fmt"
)

func main() {
    client := amps.NewClient("my-application")
    
    err := client.Connect("tcp://localhost:9007/amps/json")
    if err != nil { fmt.Println(err); return } 
    
    err = client.Logon()
    if err != nil { fmt.Println(err); return } 

    // Connected, let's subscribe to a topic now
    subID, err := client.Subscribe(func(message *amps.Message) (msgError error) {
        fmt.Println(string(message.Data()))
	return
    }, "orders")

    // Now we can publish to the above subscription
    err = client.Publish("orders", `{"order": "Tesla 3", "qty": 10}`)
}

In this example, we connect to an AMPS server running locally and initiate a subscription to the "orders" topic. As new orders are posted, myListener is invoked with each message, and prints the data of each order to the console.

Example 2: Publish a Message

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package main

import (
    "amps"
    "fmt"
)

func main() {
    client := amps.NewClient("publish-example")
    
    err := client.Connect("tcp://localhost:9000/amps/xml")
    if err != nil { fmt.Println(err); return } 
    
    err = client.Logon()
    if err != nil { fmt.Println(err); return }

    err = client.Publish("messages", "<hi>Hello, world!</hi>")
}

With AMPS, publishing is simple, as shown in this example. We connect to an AMPS server running locally, and publish a single message to the messages topic. To simply publish a message, there is no need to predeclare the topic or configure complex routing. Any subscription that has asked for XML messages on the messages topic will receive the message.

Example 3: Disconnect Handling

Rock-solid applications must be able to recover from network outages.Here our application is configured with an array of AMPS server URIs to choose from instead of a single URI. Our client is constructed and configured with the disconnect handler and an array of URIs that will be used in the `ConnectToNextUri()` function as explained below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main

import (
	"fmt"
	"time"
	"amps"
)

// The currently selected URI index (0 by default)
var currentURI int

// A list of URIs to connect
var uris = []string{
    "tcp://localhost:9000/amps/json",
    "tcp://localhost:9001/amps/json",
    "tcp://localhost:9002/amps/json",
}


/*
  This function is invoked by the disconnect handler,
  with a URI to connect.
*/
func connectToNextURI(client *amps.Client, uri string) {
    fmt.Println("Connecting to", uri, "...")

    err := client.Connect(uri)
    // Can't establish connection
    if err != nil {
        time.Sleep(time.Second)
        currentURI = (currentURI + 1) % len(uris);
        connectToNextURI(client, uris[currentURI])
        return
    }

    err = client.Logon()
    // Can't logon
    if err !=  nil {
        fmt.Println("Failed to logon")
        return
    }

    // Successfully connected
    fmt.Println("Connected!")
}


func main() {
    var client = amps.NewClient("failover-demo")

    /*
     In our disconnect handler, we invoke connectToNextURI(),
     with the next URI in our array of URIs and display
     a warning message. This simplistic handler never gives up,
     but in a typical implementation, you would likely stop
     attempting to reconnect at some point.
    */
    client.SetDisconnectHandler(func(cl *amps.Client, err error) {
        fmt.Println("Switching to the next URI...")

        currentURI = (currentURI + 1) % len(uris);
        connectToNextURI(client, uris[currentURI]);
    })

    // We begin by connecting to the first URI
    connectToNextURI(client, uris[currentURI]);
}

When the AMPS client detects a network disconnect, the reconnect function is called from the DisconnectHandler to re-establish connection, and our connectToNextURI method reconnects to the next URI available in the list. Notice that the DisconnectHandler is only called after a successful connection has been established, otherwise a connection error is returned by the Connect method of the client.

Example 4: Query the Contents of a "SOW" Topic

State-of-the-World ("SOW") topics in AMPS combine the power of a database table with the performance of a publish-subscribe system. Use the AMPS Python client to query the contents of a SOW topic.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import (
    "amps"
    "fmt"
)

func main() {
    client := amps.NewClient("my-application")
	
    err := client.Connect("tcp://localhost:9000/amps/json")
    if err != nil { fmt.Println(err); return } 
    
    err = client.Logon()
    if err != nil { fmt.Println(err); return }

    _, err := client.Sow(func(message *amps.Message) (msgErr error) {
        if command, _ := message.Command(); command == amps.CommandSOW {
            // Print the message data
            fmt.Println(string(message.Data()))
        }
        return
    }, "orders", "/symbol = 'ROL'")
	
    if err != nil { fmt.Println("SOW Query Error:", err); return }
}

This example queries for all orders for the symbol ROL, and simply prints the messages to the console.

Example 5: Command Interface

Even though AMPS clients provide the above named convenience methods for core AMPS functionality, you can use the Command object to fully customize the commands your application sends to the AMPS server. This is useful for more advanced scenarios where you need precise control over the command, in cases where you need to use an earlier version of the client to communicate with a more recent version of AMPS, or in cases where the named convenience method does not provide the options your application needs.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package main

import (
    "amps"
    "fmt"
)


func main() {
    client := amps.NewClient("my-application")

    err := client.Connect("tcp://localhost:9001/amps/json")
    if err != nil { fmt.Println(err); return } 

    err = client.Logon()
    if err != nil { fmt.Println(err); return }

    var subscribeCommand = amps.NewCommand("subscribe").SetTopic("messages").SetFilter("/id > 20")

    _, err = client.ExecuteAsync(subscribeCommand, func(message *amps.Message) (msgErr error) {
        // Print the message data
        fmt.Println(string(message.Data()))
        return
    })

    if err != nil { fmt.Println("Subscribe Error:", err); return }
}

This example enters a subscription to the "messages" topic with a filter applied.

Everything You Need

Ready to learn more? The AMPS Client for Go includes everything you need to get started. Each AMPS Client includes complete source code so you can fully learn and understand AMPS, and best integrate it into your environment.

Click here to download and start using the AMPS Client for Go.

If you need more help getting started, the 60East Technologies support and services team is ready to provide the support you need to help you CRANK UP THE AMPS.

The current version of the GoLang client is version 1.0.0.0, which supports AMPS servers version 5.3 and earlier.