qsse

package module
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2022 License: Apache-2.0 Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

QSSE logo
SSE Over QUIC

Implementation of Server Sent Events by QUIC. A faster replacement for traditional SSE over HTTP/2.

go version   license version

Installation

go get github.com/snapp-incubator/qsse

Basic Usage

// Client

import "github.com/snapp-incubator/qsse"

func main() {
    config := &qsse.ClientConfig{
        Token:     "secret",
        TLSConfig: nil,
    }
    topics := []string{"firstnames", "lastnames"}
    
    _, err := qsse.NewClient("localhost:4242", topics, config)
    if err != nil {
        panic(err)
    }
    
    select {}
}


// Server

import (
	"github.com/snapp-incubator/qsse"
	"log"
	"math/rand"
	"time"
)

var firstNames = []string{...}

var lastNames = []string{...}

func main() {
	authenticateFunc := func(token string) bool {
		log.Printf("Authenticating token: %s", token)
		return token == "secret"
	}

	topics := []string{"firstnames", "lastnames"}

	server, err := qsse.NewServer("localhost:4242", qsse.GetDefaultTLSConfig(), topics)
	if err != nil {
		panic(err)
	}
	server.SetAuthentication(authenticateFunc)

	go func() {
		for {
            if rand.NormFloat64() > 0.5 {
                server.Publish("firstnames", RandomItem(firstNames))
            } else {
                server.Publish("lastnames", RandomItem(lastNames))
            }
			<-time.After(2 * time.Second)
		}
	}()

	select {}
}

func RandomItem(items []string) []byte {
    return []byte(items[rand.Intn(len(items))])
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDefaultTLSConfig

func GetDefaultTLSConfig() *tls.Config

GetDefaultTLSConfig returns a tls.Config with the default settings for server.

func GetSimpleTLS

func GetSimpleTLS() *tls.Config

GetSimpleTLS returns a tls.Config with the default settings for client.

Types

type Client

type Client interface {
	SetEventHandler(topic string, handler func([]byte))

	SetErrorHandler(handler func(code int, data map[string]any))

	SetMessageHandler(handler func(topic string, event []byte))
}

func NewClient

func NewClient(address string, topics []string, config *ClientConfig) (Client, error)

type ClientConfig

type ClientConfig struct {
	Token           string
	TLSConfig       *tls.Config
	ReconnectPolicy *ReconnectPolicy
}

type ReconnectPolicy

type ReconnectPolicy struct {
	Retry         bool
	RetryTimes    int
	RetryInterval int // duration between retry intervals in milliseconds
}

type Server

type Server interface {
	Publish(topic string, event []byte)

	SetAuthenticator(auth.Authenticator)
	SetAuthenticatorFunc(auth.AuthenticatorFunc)

	SetAuthorizer(auth.Authorizer)
	SetAuthorizerFunc(auth.AuthorizerFunc)
}

func NewServer

func NewServer(address string, tlsConfig *tls.Config, topics []string) (Server, error)

NewServer creates a new server and listen for connections on the given address.

Directories

Path Synopsis
examples
simple/client command
simple/server command

Jump to

Keyboard shortcuts

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