API Reference

Go client example

This guide will help you set up and use the RapidMarket Go client to receive real-time market data.

Introduction

The RapidMarket Go client allows users to subscribe to and receive real-time market data. This client leverages ZeroMQ for communication and Protocol Buffers (Protobuf) for efficient data serialization.

Usage

Below is an example version of the RapidMarket Go client.

package main

import (
	"fmt"
	"os"
	"rapidmarket_client/md/pb"
	"strings"

	zmq "github.com/pebbe/zmq4"
	"google.golang.org/protobuf/proto"
)

var (
	CRLine = "\r" + strings.Repeat(" ", 64) + "\r"
)

func run(ipcpath string) error {
	// create zmq subscriber socket
	subscriber, err := zmq.NewSocket(zmq.SUB)
	if err != nil {
		return err
	}
	defer subscriber.Close()
	// connect to zmq publisher
	err = subscriber.Connect(ipcpath)
	if err != nil {
		return err
	}
	// subscribe all messages
	subscriber.SetSubscribe("")
	fmt.Println("Receiving market data...")
	for {
		// receive message
		msg, err := subscriber.RecvBytes(0)
		if err != nil {
			return err
		}
		var book pb.L2BookProto
		// parse message
		err = proto.Unmarshal(msg, &book)
		if err != nil {
			return err
		}
		bid := book.LevelsBids[0]
		ask := book.LevelsAsks[0]
		fmt.Printf("%sbid: %.5f, %.5f\task: %.5f, %.5f", CRLine, bid.Price, bid.Qty, ask.Price, ask.Qty)
		book.Reset()
	}
}

func main() {
	if len(os.Args) != 2 {
		fmt.Fprintln(os.Stderr, "Usage: rapidmarket_client <path>")
		fmt.Fprintln(os.Stderr, "Example: rapidmarket_client \"tcp://localhost:50000\"")
		os.Exit(1)
	}
	ipcpath := os.Args[1]
	run(ipcpath)
}

Requirements

To use the RapidMarket Go client to receive data, you need the following environment and tools:

  1. Install the Go programming language:
    Ensure that Go is installed on your system. If not, follow the instructions on the official Go website to download and install it.
  2. Set up your Go environment:
    Make sure your Go environment is correctly set up. You may need to set the GOPATH and add the Go bin directory to your PATH.
    export GOPATH=$HOME/go
    export PATH=$PATH:$GOPATH/bin
    
  3. Install the Protobuf compiler:
    The Protobuf compiler is used to compile .proto files into Go code.
    sudo apt install -y protobuf-compiler
    
  4. Install the Protobuf compiler plugin for Go:
    This plugin generates Go code from Protobuf definitions.
    go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
    
  5. Install Go modules for Protobuf and ZeroMQ:
    Use go get to install the necessary Go modules for Protobuf and ZeroMQ.
    go get -u github.com/golang/protobuf/proto
    go get -u github.com/pebbe/zmq4
    
  6. Clean up Go modules:
    Use go mod tidy to remove any unused dependencies and ensure that go.mod and go.sum are up-to-date.
    go mod tidy
    

Summary of Dependencies

These commands will install the following dependencies:

  • protobuf-compiler: Compiler for compiling Protobuf files.
  • google.golang.org/protobuf/cmd/protoc-gen-go: Plugin for generating Go code from Protobuf definitions.
  • github.com/golang/protobuf/proto: Go bindings for Protocol Buffers.
  • github.com/pebbe/zmq4: Go bindings for ZeroMQ, used for communication with RapidMarket.

Getting Started

For more detailed information on ZeroMQ and Protobuf, including their roles in the RapidMarket application, please refer to the Getting Started page.

Directory structure

.
├── README*.md         Client example documentation
├── SERVER*.md         Server documentation
├── go.mod            Go Mod package management file
├── go.sum            Go Mod package management file
├── l2_book.proto     Protobuf definition file
└── main.go           Client example code

Compilation

# Compile the `md/pb/l2_book.pb.go` file required by Proto Buffer
protoc --go_out=. ./l2_book.proto
# Compile the example code
go build -o bin/rapidmarket_client .

Start command

IP_ADDR and PORT below are the IP address and port of the RapidMarket server, respectively.

cd bin
./rapidmarket_client "tcp://${IP_ADDR}:${PORT}"
# For example ./rapidmarket_client "tcp://192.168.0.1:50000"

The program outputs real-time market data:

Receiving market data...
bid: 30033.05000, 6.48043       ask: 30033.06000, 4.91876

l2_book.proto File Explanation

The message format released by RapidMarket is defined in l2_book.proto. It outlines the hierarchical structure and fields of market data, aiding clients in correctly parsing received market data.