Go Client

Forest Bus Go Client Library

The Go (golang) client for Forest Bus is available using go get github.com/owlfish/forestbus

The library has no dependencies and, unlike the Java and Python libraries, can connect directly to the port used in the "-name" parameter when starting forest-bus-server.

6th September 2015: Note that the repository has moved to Github as Google have shutdown their hosting for open source projects.

Documentation

The library uses godoc for documentation, a copy of which is available on the GoDoc website.

Quick Start

/*
This code assumes a three node cluster is running on localhost, configured as follows:

# forest-bus-server -id testcluster -cbor :5000 -name :3000 -path ~/forest-data/bus1
# forest-bus-server -id testcluster -cbor :5001 -name :3001 -path ~/forest-data/bus2
# forest-bus-server -id testcluster -cbor :5002 -name :3002 -path ~/forest-data/bus3

On first start the servers need configuring with their peers:
# forest-admin -id testcluster peers :3000,:3001,:3002

A test topic also needs to have been created:
# forest-admin -id testcluster topic test
*/
package main

import (
	"code.google.com/p/forestbus"
	"fmt"
	"os"
)

func main() {
	// Create a client to connect
	client := forestbus.NewClient("testcluster", []string{"localhost:3000", "localhost:3001", "localhost:3002"})
	testMsg := [][]byte{[]byte("Test Message from Go!"), []byte("Second message from Go in the same batch.")}
	indexes, err := client.SendMessages("test", testMsg, true)
	if err != nil {
		fmt.Printf("Error sending messages to forest bus: %v\n", err)
		panic("Done.")
	}
	fmt.Printf("Sent messages with index %v and %v\n", indexes[0], indexes[1])

	receivedMsgs, nextID, err := client.GetMessages("test", 1, 1, true)
	if err != nil {
		fmt.Printf("Error getting messages from forest bus: %v\n", err)
		panic("Done.")
	}

	fmt.Printf("Messages received (next index %v):\n", nextID)
	for _, msg := range receivedMsgs {
		os.Stdout.Write(msg)
		os.Stdout.Write([]byte("\n"))
	}
	client.Close()
}
Last Modified: Sun, 06 Sep 2015 16:58:27 BST

Made with PubTal 3.5

Copyright 2017 Colin Stewart

Email: colin at owlfish.com