centrifugo

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package centrifugo wraps the Centrifugo real-time messaging client, exposing a server-side publisher and a subscriber client.

Index

Constants

View Source
const (
	ErrCodeCentrifugoConnect        = "CTRF-001"
	ErrCodeCentrifugoPublish        = "CTRF-002"
	ErrCodeCentrifugeInternal       = "CTRF-004"
	ErrCodeCentrifugoSubscribing    = "CTRF-005"
	ErrCodeCentrifugoSubscribe      = "CTRF-006"
	ErrCodeGrpcServerConnect        = "CTRF-007"
	ErrCodeGrpcServerPublish        = "CTRF-008"
	ErrCodeGrpcServerPublishRs      = "CTRF-009"
	ErrCodeGrpcServerBatchPublish   = "CTRF-010"
	ErrCodeGrpcServerBatchPublishRs = "CTRF-011"
	ErrCodeGrpcServerDisconnect     = "CTRF-012"
	ErrCodeGrpcServerDisconnectRs   = "CTRF-013"
)

Variables

View Source
var (
	ErrCentrifugoConnect = func(ctx context.Context, cause error) error {
		return jet.NewAppErrBuilder(ErrCodeCentrifugoConnect, "centrifugo: connect").Wrap(cause).Err()
	}
	ErrCentrifugoGrpcPublish = func(ctx context.Context, cause error) error {
		return jet.NewAppErrBuilder(ErrCodeCentrifugoPublish, "centrifugo: publish").Wrap(cause).Err()
	}
	ErrCentrifugeInternal = func(ctx context.Context, cause error) error {
		return jet.NewAppErrBuilder(ErrCodeCentrifugeInternal, "centrifugo error").Wrap(cause).Err()
	}
	ErrCentrifugoSubscribing = func(ctx context.Context, cause error) error {
		return jet.NewAppErrBuilder(ErrCodeCentrifugoSubscribing, "centrifugo: subscribing").Wrap(cause).Err()
	}
	ErrCentrifugoSubscribe = func(ctx context.Context, cause error) error {
		return jet.NewAppErrBuilder(ErrCodeCentrifugoSubscribe, "centrifugo: subscribe").Wrap(cause).Err()
	}
	ErrGrpcServerConnect = func(ctx context.Context, cause error) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcServerConnect, "centrifugo server: connect").Wrap(cause).Err()
	}
	ErrGrpcServerPublish = func(ctx context.Context, cause error) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcServerPublish, "centrifugo server: publish").Wrap(cause).Err()
	}
	ErrGrpcServerPublishRs = func(ctx context.Context, cause *apiproto.Error) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcServerPublishRs, "centrifugo server: publish %s (%d)", cause.Message, cause.Code).Err()
	}
	ErrGrpcServerPresence = func(ctx context.Context, cause error) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcServerPublish, "centrifugo server: presence").Wrap(cause).Err()
	}
	ErrGrpcServerPresenceRs = func(ctx context.Context, cause *apiproto.Error) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcServerPublishRs, "centrifugo server: presence %s (%d)", cause.Message, cause.Code).Err()
	}
	ErrGrpcServerBatchPublish = func(ctx context.Context, cause error) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcServerBatchPublish, "centrifugo server: batch publish").Wrap(cause).Err()
	}
	ErrGrpcServerBatchPublishRs = func(ctx context.Context, cause *apiproto.Error) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcServerBatchPublishRs, "centrifugo server: batch publish %s (%d)", cause.Message, cause.Code).Err()
	}
	ErrGrpcServerDisconnect = func(ctx context.Context, cause error) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcServerDisconnect, "centrifugo server: disconnect user").Wrap(cause).Err()
	}
	ErrGrpcServerDisconnectRs = func(ctx context.Context, cause *apiproto.Error) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcServerDisconnectRs, "centrifugo server: disconnect user %s (%d)", cause.Message, cause.Code).Err()
	}
)

Functions

func GenerateConnectToken

func GenerateConnectToken(ctx context.Context, secret, userId string, autoSubscribeChannels []string, ttl time.Duration, info any) (string, error)

GenerateConnectToken generates a client connect token

func GenerateSubscribeToken

func GenerateSubscribeToken(ctx context.Context, secret, userId, channel string, ttl time.Duration) (string, error)

GenerateSubscribeToken generates a client subscribe token

Types

type Client

type Client interface {
	// Connect connects client
	Connect(ctx context.Context, token string) error
	// Close closes connection
	Close(ctx context.Context) error
	// Subscribe subscribes on channel. Does not require in case of server-side subscription
	Subscribe(ctx context.Context, token, channel string, handler func(p centrifuge.Publication) error) error
	// OnPublication allows handling publication in case of server-side subscription
	OnPublication(ctx context.Context, handler func(p centrifuge.ServerPublicationEvent) error) error
	// Connected returns true if client is connected
	Connected() bool
	// ClientId returns client id of a connected client
	ClientId() string
}

func NewClient

func NewClient(cfg *ClientConfig, logger jet.CLoggerFunc) Client

type ClientConfig

type ClientConfig struct {
	Url string
}

type Event

type Event[T any] struct {
	Type string    `json:"type"`
	Ts   time.Time `json:"ts"`
	Body T         `json:"body"`
}

type Server

type Server interface {
	// Connect connects to grpc server
	Connect(ctx context.Context) error
	// DisconnectUser disconnects user by id
	DisconnectUser(ctx context.Context, userId string) error
	// Close closes connection
	Close(ctx context.Context) error
	// Publish publishes message to channel
	Publish(ctx context.Context, channel string, msg any) error
	// GetPresence retrieves presence by channel
	GetPresence(ctx context.Context, channel string) (*apiproto.PresenceResult, error)
	// BatchPublish publishes same message in several channels
	BatchPublish(ctx context.Context, channels []string, msg any) error
}

func NewServer

func NewServer(cfg *ServerConfig, logger jet.CLoggerFunc) Server

type ServerConfig

type ServerConfig struct {
	Host   string // Host grpc host
	Port   string // Port grpc port
	ApiKey string `mapstructure:"api_key"` // ApiKey to connect to grpc server
	Secret string // Secret used for token generating
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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