sseclient

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

SSE stands for Server-Sent Events. Server-Sent Events is a technology, that the server pushes the data to the client, the client receives automatic updates from the server via an HTTP connection.

ctrlX Data Layer has implemented a SSE Server, which provides data access via SSE protocol.

Package sseclient implements a SSE Client, which is used to connect the SSE server on the ctrlX Data Layer.

Before we create SseClient, two things are needed:

1) Get authorization token The token is a Bearer token, which is provied by ctrlX Identity Manager Component. 2) Create SSE subscription on the SSE Server on the Data Layer The subscriptionID is generated at first on client side by using for example package uuid, this id then will be sent to the server together with other SSE parameters especially nodelist by sending a POST request to the ctrlX Data Layer. The subscription then will be created on the server side, and it can be accessed by using the subscriptionID.

Let us now create a SseClient:

The function NewSseClient needs three parameters: * the url is like "/automation/api/v2/events/<subscriptionID>", * the token is like "Bearer eyJhbGciOiJIUxxx" * the flag insecureSkipVerify has to be set to true if no corresponding certificate has been installed on the ctrlX CORE, which is usually not the case in delivery state.

client := sseclient.NewSseClient(url, token, true)

Reading event data:

The function Subscribe starts GET request and then reads the event data continuouslly. * The paramter context is used to cancell the reading loop * The callback function of type SseReceiverFunc is used to handle the incoming event data.

This function will only terminate in case of connection errors (err != nil) or if the subscription is terminated by the main program side (err == nil).

  err = client.Subscribe(ctx, func(event string, data string) {
		...
	})

The SSE Server sends following types of event and data

"event: update\n" "id: ...\n" "data: {"node":".....","timestamp":....,"type":"...","value":...}\n" "\n"

"event: error\n" "id: ...\n" "data: {"type":".....","title":....,"status":...,"instance":"...","severity":""}\n" "\n"

"event: keepalive\n" "id: ...\n" "data: {"timestamp":....}\n" "\n"

The new subscription should be created and the connection should be restablished if it is broken, the SSE Server on the ctrlX Data Layer will delete the inactive existing subscription after a time period.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SseClient

type SseClient struct {
	URL        string
	Connection *http.Client
	Headers    map[string]string
}

SseClient is an instance of an SSE-Client sending events.

func NewSseClient

func NewSseClient(url string, authorization string, insecureSkipVerify bool) *SseClient

NewSseClient creates a new client.

func (*SseClient) Subscribe

func (c *SseClient) Subscribe(ctx context.Context, receiver SseReceiverFunc) error

Subscribe starts the event stream and reads the incoming data. Data is parsed as Sse Event and forwarded to the given callback func.

type SseEvent

type SseEvent struct {
	Event string
	Data  string
}

SseEvent is a go representation of an http server-sent event.

type SseReceiverFunc

type SseReceiverFunc func(event string, data string)

SseReceiverFunc defines the callback function that will be called on new event data.

Jump to

Keyboard shortcuts

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