Documentation
¶
Overview ¶
Package KSbus is a minimalistic event bus demonstration on how useful is to design event based systems using Golang, allowing you to synchronise your backends and frontends
What is included in this package ? ¶
This package use [Kmux] wich is the same router as Korm
KSbus can be used between your go servers, and between your servers and client using the built in JS library ¶
Index ¶
- func GenerateRandomBytes(n int) ([]byte, error)
- func GenerateRandomString(s int) string
- func GenerateUUID() string
- func RestartSelf() error
- func RetryEvery(t time.Duration, function func() error, maxRetry ...int)
- func RunEvery(t time.Duration, fn func() bool)
- type Bus
- func (b *Bus) Publish(topic string, data map[string]any)
- func (b *Bus) PublishToID(id string, data map[string]any)
- func (b *Bus) PublishToIDWaitRecv(id string, data map[string]any, onRecv func(data map[string]any), ...) error
- func (b *Bus) PublishWaitRecv(topic string, data map[string]any, onRecv func(data map[string]any), ...) error
- func (b *Bus) RemoveTopic(topic string)
- func (b *Bus) Subscribe(topic string, fn func(data map[string]any, unsub Unsub), ...) Unsub
- func (b *Bus) Unsubscribe(topic string)
- type BusRPC
- func (b *BusRPC) Ping(req *RPCRequest, resp *RPCResponse) error
- func (b *BusRPC) Poll(req *RPCRequest, resp *RPCResponse) error
- func (b *BusRPC) Publish(req *RPCRequest, resp *RPCResponse) error
- func (b *BusRPC) PublishToID(req *RPCRequest, resp *RPCResponse) error
- func (b *BusRPC) RemoveTopic(req *RPCRequest, resp *RPCResponse) error
- func (b *BusRPC) Subscribe(req *RPCRequest, resp *RPCResponse) error
- func (b *BusRPC) Unsubscribe(req *RPCRequest, resp *RPCResponse) error
- type Client
- func (client *Client) Close() error
- func (client *Client) OnClose(fn func())
- func (client *Client) Publish(topic string, data map[string]any)
- func (client *Client) PublishToID(id string, data map[string]any)
- func (client *Client) PublishToIDWaitRecv(id string, data map[string]any, onRecv func(data map[string]any), ...)
- func (client *Client) PublishToServer(addr string, data map[string]any, secure ...bool)
- func (client *Client) PublishWaitRecv(topic string, data map[string]any, onRecv func(data map[string]any), ...)
- func (client *Client) RemoveTopic(topic string)
- func (client *Client) Run()
- func (client *Client) Subscribe(topic string, handler func(data map[string]any, unsub ClientSubscriber)) ClientSubscriber
- func (client *Client) Unsubscribe(topic string)
- type ClientConnectOptions
- type ClientSubscriber
- type RPCClient
- func (c *RPCClient) Close() error
- func (c *RPCClient) OnClose(fn func())
- func (c *RPCClient) Publish(topic string, data map[string]any)
- func (c *RPCClient) PublishToID(id string, data map[string]any)
- func (c *RPCClient) PublishToIDWaitRecv(id string, data map[string]any, onRecv func(data map[string]any), ...)
- func (c *RPCClient) PublishWaitRecv(topic string, data map[string]any, onRecv func(data map[string]any), ...)
- func (c *RPCClient) RemoveTopic(topic string)
- func (c *RPCClient) Run()
- func (c *RPCClient) Subscribe(topic string, handler func(data map[string]any, unsub RPCSubscriber)) RPCSubscriber
- func (c *RPCClient) Unsubscribe(topic string)
- type RPCClientOptions
- type RPCConn
- type RPCRequest
- type RPCResponse
- type RPCSubscriber
- type Server
- func (server *Server) AllTopics() []string
- func (s *Server) EnableRPC(address string) error
- func (s *Server) GetSubscribers(topic string) []Subscriber
- func (s *Server) OnDataWs(...)
- func (s *Server) OnId(fn func(data map[string]any))
- func (s *Server) OnServerData(fn func(data any, conn *ws.Conn))
- func (s *Server) OnWsClose(fn func(connID string))
- func (srv *Server) Publish(topic string, data map[string]any)
- func (s *Server) PublishToID(id string, data map[string]any)
- func (s *Server) PublishToIDWaitRecv(id string, data map[string]any, onRecv func(data map[string]any), ...)
- func (s *Server) PublishToServer(addr string, data map[string]any, secure ...bool) error
- func (s *Server) PublishWaitRecv(topic string, data map[string]any, onRecv func(data map[string]any), ...)
- func (s *Server) RemoveTopic(topic string)
- func (s *Server) Run()
- func (s *Server) RunAutoTLS()
- func (s *Server) RunTLS()
- func (s *Server) SetRPCMaxQueueSize(size int)
- func (s *Server) Subscribe(topic string, fn func(data map[string]any, unsub Unsub)) (unsub Unsub)
- func (s *Server) Unsubscribe(topic string)
- func (s *Server) WithMetrics(httpHandler http.Handler, path ...string)
- func (s *Server) WithPprof(path ...string)
- type ServerOpts
- type Subscriber
- type Unsub
- type WithRpc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateRandomString ¶
func GenerateUUID ¶ added in v1.1.8
func GenerateUUID() string
func RestartSelf ¶ added in v0.9.4
func RestartSelf() error
Types ¶
type Bus ¶
type Bus struct {
// contains filtered or unexported fields
}
Bus type handle all subscriptions, websockets and channels
func (*Bus) PublishToIDWaitRecv ¶ added in v1.2.5
func (*Bus) PublishWaitRecv ¶ added in v0.9.7
func (*Bus) RemoveTopic ¶
func (*Bus) Unsubscribe ¶
type BusRPC ¶ added in v1.3.8
type BusRPC struct {
// contains filtered or unexported fields
}
func (*BusRPC) Ping ¶ added in v1.3.8
func (b *BusRPC) Ping(req *RPCRequest, resp *RPCResponse) error
func (*BusRPC) Poll ¶ added in v1.3.8
func (b *BusRPC) Poll(req *RPCRequest, resp *RPCResponse) error
func (*BusRPC) Publish ¶ added in v1.3.8
func (b *BusRPC) Publish(req *RPCRequest, resp *RPCResponse) error
func (*BusRPC) PublishToID ¶ added in v1.3.8
func (b *BusRPC) PublishToID(req *RPCRequest, resp *RPCResponse) error
func (*BusRPC) RemoveTopic ¶ added in v1.3.8
func (b *BusRPC) RemoveTopic(req *RPCRequest, resp *RPCResponse) error
func (*BusRPC) Subscribe ¶ added in v1.3.8
func (b *BusRPC) Subscribe(req *RPCRequest, resp *RPCResponse) error
func (*BusRPC) Unsubscribe ¶ added in v1.3.8
func (b *BusRPC) Unsubscribe(req *RPCRequest, resp *RPCResponse) error
type Client ¶
type Client struct {
Id string
ServerAddr string
RestartEvery time.Duration
Conn *ws.Conn
Autorestart bool
Done chan struct{}
// contains filtered or unexported fields
}
func NewClient ¶
func NewClient(opts ClientConnectOptions) (*Client, error)
func (*Client) PublishToID ¶ added in v1.1.8
func (*Client) PublishToIDWaitRecv ¶ added in v1.2.0
func (*Client) PublishToServer ¶ added in v1.2.0
func (*Client) PublishWaitRecv ¶ added in v1.2.0
func (*Client) RemoveTopic ¶
func (*Client) Subscribe ¶
func (client *Client) Subscribe(topic string, handler func(data map[string]any, unsub ClientSubscriber)) ClientSubscriber
func (*Client) Unsubscribe ¶
type ClientConnectOptions ¶ added in v1.1.8
type ClientSubscriber ¶ added in v1.3.6
type ClientSubscriber struct {
Id string
Topic string
Ch chan map[string]any
Conn *ws.Conn
// contains filtered or unexported fields
}
func (ClientSubscriber) Unsubscribe ¶ added in v1.3.6
func (subs ClientSubscriber) Unsubscribe()
type RPCClient ¶ added in v1.3.8
type RPCClient struct {
Id string
ServerAddr string
Autorestart bool
RestartEvery time.Duration
Done chan struct{}
// contains filtered or unexported fields
}
RPCClient implements a client that connects to the bus system using RPC
func NewRPCClient ¶ added in v1.3.8
func NewRPCClient(opts RPCClientOptions) (*RPCClient, error)
NewRPCClient creates a new RPC client connection to the bus
func (*RPCClient) OnClose ¶ added in v1.4.1
func (c *RPCClient) OnClose(fn func())
OnClose sets the callback function to be called when the connection is closed
func (*RPCClient) PublishToID ¶ added in v1.3.8
func (*RPCClient) PublishToIDWaitRecv ¶ added in v1.3.8
func (*RPCClient) PublishWaitRecv ¶ added in v1.3.8
func (*RPCClient) RemoveTopic ¶ added in v1.3.8
func (*RPCClient) Subscribe ¶ added in v1.3.8
func (c *RPCClient) Subscribe(topic string, handler func(data map[string]any, unsub RPCSubscriber)) RPCSubscriber
func (*RPCClient) Unsubscribe ¶ added in v1.3.8
type RPCClientOptions ¶ added in v1.3.8
type RPCConn ¶ added in v1.4.0
type RPCConn struct {
Id string
// contains filtered or unexported fields
}
type RPCRequest ¶ added in v1.3.8
RPCRequest represents the data structure for RPC calls
type RPCResponse ¶ added in v1.3.8
RPCResponse represents the response from RPC calls
type RPCSubscriber ¶ added in v1.3.8
RPCSubscriber represents a subscription to a topic via RPC
func (RPCSubscriber) Unsubscribe ¶ added in v1.3.8
func (s RPCSubscriber) Unsubscribe()
type Server ¶
type Server struct {
ID string
Address string
Path string
Bus *Bus
App *ksmux.Router
// contains filtered or unexported fields
}
func NewServer ¶
func NewServer(options ...ServerOpts) *Server
func (*Server) GetSubscribers ¶ added in v1.1.8
func (s *Server) GetSubscribers(topic string) []Subscriber
func (*Server) OnServerData ¶ added in v1.2.3
func (*Server) PublishToID ¶ added in v1.1.8
func (*Server) PublishToIDWaitRecv ¶ added in v1.2.0
func (*Server) PublishToServer ¶ added in v1.2.0
func (*Server) PublishWaitRecv ¶ added in v0.9.7
func (*Server) RemoveTopic ¶
func (*Server) RunAutoTLS ¶
func (s *Server) RunAutoTLS()
func (*Server) SetRPCMaxQueueSize ¶ added in v1.3.8
func (*Server) Unsubscribe ¶
func (*Server) WithMetrics ¶ added in v1.0.2
type ServerOpts ¶ added in v1.3.3
type ServerOpts struct {
ID string
Address string
BusPath string
BusMidws []func(ksmux.Handler) ksmux.Handler
OnWsClose func(connID string)
OnDataWS func(data map[string]any, conn *ws.Conn, originalRequest *http.Request) error
OnServerData []func(data any, conn *ws.Conn)
OnId func(data map[string]any)
OnUpgradeWs func(r *http.Request) bool
WithRPCAddress string
WithOtherRouter *ksmux.Router
WithOtherBus *Bus
}
func NewDefaultServerOptions ¶ added in v1.3.3
func NewDefaultServerOptions() ServerOpts
type Subscriber ¶ added in v1.2.6
type Subscriber struct {
Id string
Topic string
Ch chan map[string]any
Conn *ws.Conn
// contains filtered or unexported fields
}
func (Subscriber) Unsubscribe ¶ added in v1.2.6
func (subs Subscriber) Unsubscribe()