eventstream

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

README

Build Status

eventstream-go-sdk

Go SDK for integrating with AccelByte's event stream

Usage

Install
go get -u github.com/AccelByte/eventstream-go-sdk
Importing
eventstream "github.com/AccelByte/eventstream-go-sdk"

Supported Stream

Currently event stream are supported by these stream:

Kafka Stream

Publish and subscribe an event to / from Kafka stream.

currently compatible with golang version from 1.12+ and Kafka versions from 0.10.1.0 to 2.1.0.

To create a new Kafka stream client, use this function:

client, err := NewKafkaClient(brokers []string, prefix string, config ...*KafkaConfig)

NewKafkaClient requires 3 parameters :

  • brokers : List of kafka broker (array of string)
  • prefix : Topic prefix from client (string)
  • config : Custom kafka configuration from client. This is optional and only uses the first arguments. (variadic KafkaConfig)
Publish

Publish or sent an event into kafka stream. Client able to publish an event into single or multiple topic. Publish support with exponential backoff retry. (max 3x)

To publish an event, use this function:

client.Publish(
		NewPublish().
			Topic(TopicName).
			EventName(EventName).
			Namespace(Namespace).
			ClientID(ClientID).
			UserID(UserID).
			TraceID(TraceID).
			Context(Context).
			Payload(Payload))
Subscribe

To subscribe an event from specific topic, client should be register a callback function that executed once event received. A callback function has specific topic and event name.

To subscribe an event, use this function:

client.Register(
		NewSubscribe().
			Topic(topicName).
			EventName(mockEvent.EventName).
			Context(Context).
			Callback(func(event *Event, err error) {}))

Note: Callback function should be func(event *Event, err error){}. event is object that store event message and err is an error that happen when consume the message.

Custom Configuration

SDK support with custom configuration for kafka stream, that is :

  • DialTimeout : Timeout duration during connecting to kafka broker (time.Duration)
  • ReadTimeout : Timeout duration during consume topic from kafka broker (time.Duration)
  • WriteTimeout : Timeout duration during publish event to kafka broker (time.Duration)
Stdout Stream

This stream is for testing purpose. This will print the event in stdout. It should not be used in production since this will print unnecessary log.

To create a client for stdout, use this function:

client, err := NewStdoutClient(prefix string)
Blackhole

This is used when client don't want the service to send event data to anywhere.

To create a client for stdout, use this function:

client, err := NewBlackholeClient()

Event Message

Event message is a set of event information that would be publish or consume by client.

Event message format :

  • id : Event ID with UUID format (string)
  • name : Event name (string)
  • namespace : Event namespace (string)
  • traceId : Trace ID (string)
  • clientId : Publisher client ID (string)
  • userId : Publisher user ID (string)
  • timestamp : Event time (time.Time)
  • version : Event schema version (string)
  • payload : Set of data / object that given by producer. Each data have own key for specific purpose. (map[string]interface{})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlackholeClient

type BlackholeClient struct{}

BlackholeClient satisfies the publisher for mocking

func NewBlackholeClient

func NewBlackholeClient() (*BlackholeClient, error)

NewBlackholeClient creates new telemetry client

func (*BlackholeClient) Publish added in v1.0.0

func (client *BlackholeClient) Publish(publishBuilder *PublishBuilder)

func (*BlackholeClient) Register added in v1.0.0

func (client *BlackholeClient) Register(subscribeBuilder *SubscribeBuilder)

type Client added in v1.0.0

type Client interface {
	Publish(publishBuilder *PublishBuilder)
	Register(subscribeBuilder *SubscribeBuilder)
}

Client is an interface for event stream functionality

func NewClient added in v1.0.0

func NewClient(prefix, stream string, brokers []string) (Client, error)

type Event

type Event struct {
	ID        string                 `json:"id"`
	EventName string                 `json:"name"`
	Namespace string                 `json:"namespace"`
	ClientID  string                 `json:"clientId"`
	TraceID   string                 `json:"traceId"`
	UserID    string                 `json:"userId"`
	Timestamp string                 `json:"timestamp"`
	Version   string                 `json:"version"`
	Payload   map[string]interface{} `json:"payload"`
}

Event defines the structure of event

type KafkaClient

type KafkaClient struct {
	// contains filtered or unexported fields
}

KafkaClient wraps client's functionality for Kafka

func NewKafkaClient

func NewKafkaClient(brokers []string, prefix string, config ...*KafkaConfig) (*KafkaClient, error)

NewKafkaClient create a new instance of KafkaClient

func (*KafkaClient) Publish added in v1.0.0

func (client *KafkaClient) Publish(publishBuilder *PublishBuilder)

Publish send event to single or multiple topic with exponential backoff retry

func (*KafkaClient) Register added in v1.0.0

func (client *KafkaClient) Register(subscribeBuilder *SubscribeBuilder)

Register register callback function and then subscribe topic

type KafkaConfig

type KafkaConfig struct {
	DialTimeout  time.Duration
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
}

KafkaConfig is Kafka configuration to wait dial connection, read and write process

type PublishBuilder added in v1.0.0

type PublishBuilder struct {
	// contains filtered or unexported fields
}

PublishBuilder defines the structure of message which is sent through message broker

func NewPublish added in v1.0.0

func NewPublish() *PublishBuilder

NewPublish create new PublishBuilder instance

func (*PublishBuilder) ClientID added in v1.0.0

func (p *PublishBuilder) ClientID(clientID string) *PublishBuilder

ClientID set clientID of publisher event

func (*PublishBuilder) Context added in v1.0.0

func (p *PublishBuilder) Context(ctx context.Context) *PublishBuilder

Context define client context when publish event. default: context.Background()

func (*PublishBuilder) EventName added in v1.0.0

func (p *PublishBuilder) EventName(eventName string) *PublishBuilder

EventName set name of published event

func (*PublishBuilder) Namespace added in v1.0.0

func (p *PublishBuilder) Namespace(namespace string) *PublishBuilder

Namespace set namespace of published event

func (*PublishBuilder) Payload added in v1.0.0

func (p *PublishBuilder) Payload(payload map[string]interface{}) *PublishBuilder

Payload is a event payload that will be published

func (*PublishBuilder) Topic added in v1.0.0

func (p *PublishBuilder) Topic(topics ...string) *PublishBuilder

Topic set channel / topic name

func (*PublishBuilder) TraceID added in v1.0.0

func (p *PublishBuilder) TraceID(traceID string) *PublishBuilder

TraceID set traceID of publisher event

func (*PublishBuilder) UserID added in v1.0.0

func (p *PublishBuilder) UserID(userID string) *PublishBuilder

UserID set userID of publisher event

func (*PublishBuilder) Version added in v1.0.0

func (p *PublishBuilder) Version(version string) *PublishBuilder

Version set event schema version

type StdoutClient

type StdoutClient struct {
	// contains filtered or unexported fields
}

StdoutClient satisfies the publisher for mocking

func NewStdoutClient

func NewStdoutClient(prefix string) (*StdoutClient, error)

NewStdoutClient creates new telemetry client

func (*StdoutClient) Publish added in v1.0.0

func (client *StdoutClient) Publish(publishBuilder *PublishBuilder)

Publish print event to console

func (*StdoutClient) Register added in v1.0.0

func (client *StdoutClient) Register(subscribeBuilder *SubscribeBuilder)

Register print event to console

type SubscribeBuilder added in v1.0.0

type SubscribeBuilder struct {
	// contains filtered or unexported fields
}

SubscribeBuilder defines the structure of message which is sent through message broker

func NewSubscribe added in v1.0.0

func NewSubscribe() *SubscribeBuilder

NewSubscribe create new SubscribeBuilder instance

func (*SubscribeBuilder) Callback added in v1.0.0

func (s *SubscribeBuilder) Callback(callback func(event *Event, err error)) *SubscribeBuilder

Callback to do when the event received

func (*SubscribeBuilder) Context added in v1.0.0

Context define client context when subscribe event. default: context.Background()

func (*SubscribeBuilder) EventName added in v1.0.0

func (s *SubscribeBuilder) EventName(eventName string) *SubscribeBuilder

EventName set event name that will be subscribe

func (*SubscribeBuilder) Topic added in v1.0.0

func (s *SubscribeBuilder) Topic(topic string) *SubscribeBuilder

Topic set topic that will be subscribe

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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