qsse

module
v1.0.2-beta Latest Latest
Warning

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

Go to latest
Published: May 17, 2022 License: Apache-2.0

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

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))])
}

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