Documentation
¶
Overview ¶
Package emitter provides broadcast capabilities for Socket.IO via MongoDB.
Package emitter provides an API for broadcasting messages to Socket.IO servers via MongoDB without requiring a full Socket.IO server instance.
Package emitter provides types and interfaces for broadcasting messages to Socket.IO servers using MongoDB.
Package emitter provides an API for broadcasting messages to Socket.IO servers via MongoDB without requiring a full Socket.IO server instance.
This is useful for sending messages from other processes or services that don't run a Socket.IO server but need to communicate with connected clients.
Index ¶
- Constants
- type BroadcastMessage
- type BroadcastOperator
- func (b *BroadcastOperator) Compress(compress bool) BroadcastOperatorInterface
- func (b *BroadcastOperator) Construct(client *mongo.MongoClient, broadcastOptions *BroadcastOptions, ...)
- func (b *BroadcastOperator) DisconnectSockets(state bool) error
- func (b *BroadcastOperator) Emit(ev string, args ...any) error
- func (b *BroadcastOperator) Except(room ...socket.Room) BroadcastOperatorInterface
- func (b *BroadcastOperator) In(room ...socket.Room) BroadcastOperatorInterface
- func (b *BroadcastOperator) ServerSideEmit(args ...any) error
- func (b *BroadcastOperator) SocketsJoin(rooms ...socket.Room) error
- func (b *BroadcastOperator) SocketsLeave(rooms ...socket.Room) error
- func (b *BroadcastOperator) To(room ...socket.Room) BroadcastOperatorInterface
- func (b *BroadcastOperator) Volatile() BroadcastOperatorInterface
- type BroadcastOperatorInterface
- type BroadcastOptions
- type ClusterMessage
- type DisconnectSocketsMessage
- type Emitter
- func (e *Emitter) Compress(compress bool) BroadcastOperatorInterface
- func (e *Emitter) Construct(client *mongo.MongoClient, opts *EmitterOptions, nsps ...string)
- func (e *Emitter) DisconnectSockets(state bool) error
- func (e *Emitter) Emit(ev string, args ...any) error
- func (e *Emitter) Except(rooms ...socket.Room) BroadcastOperatorInterface
- func (e *Emitter) In(rooms ...socket.Room) BroadcastOperatorInterface
- func (e *Emitter) Of(nsp string) *Emitter
- func (e *Emitter) ServerSideEmit(args ...any) error
- func (e *Emitter) SocketsJoin(rooms ...socket.Room) error
- func (e *Emitter) SocketsLeave(rooms ...socket.Room) error
- func (e *Emitter) To(rooms ...socket.Room) BroadcastOperatorInterface
- func (e *Emitter) Volatile() BroadcastOperatorInterface
- type EmitterOptions
- type EmitterOptionsInterface
- type ServerSideEmitMessage
- type SocketsJoinLeaveMessage
Constants ¶
const ( // DefaultEmitterKey is a placeholder to match the interface pattern. // The MongoDB emitter does not use channel prefixes since it uses a single collection. DefaultEmitterKey = "socket.io" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BroadcastMessage ¶
type BroadcastMessage = adapter.BroadcastMessage
BroadcastMessage is an alias for adapter.BroadcastMessage. It is used for broadcasting.
type BroadcastOperator ¶
type BroadcastOperator struct {
// contains filtered or unexported fields
}
BroadcastOperator provides a fluent API for broadcasting events to Socket.IO clients via MongoDB. It supports room targeting, exclusions, and broadcast flags through method chaining.
func MakeBroadcastOperator ¶
func MakeBroadcastOperator() *BroadcastOperator
MakeBroadcastOperator creates a new BroadcastOperator with empty room sets and default flags.
func NewBroadcastOperator ¶
func NewBroadcastOperator( client *mongo.MongoClient, broadcastOptions *BroadcastOptions, rooms *types.Set[socket.Room], exceptRooms *types.Set[socket.Room], flags *socket.BroadcastFlags, ) *BroadcastOperator
NewBroadcastOperator creates and initializes a new BroadcastOperator with the given configuration. Nil parameters are replaced with safe defaults.
func (*BroadcastOperator) Compress ¶
func (b *BroadcastOperator) Compress(compress bool) BroadcastOperatorInterface
Compress sets the compress flag for the broadcast. When true, the message will be compressed before transmission.
func (*BroadcastOperator) Construct ¶
func (b *BroadcastOperator) Construct( client *mongo.MongoClient, broadcastOptions *BroadcastOptions, rooms *types.Set[socket.Room], exceptRooms *types.Set[socket.Room], flags *socket.BroadcastFlags, )
Construct initializes the BroadcastOperator with the given parameters. This method is called by NewBroadcastOperator and handles nil safety.
func (*BroadcastOperator) DisconnectSockets ¶
func (b *BroadcastOperator) DisconnectSockets(state bool) error
DisconnectSockets disconnects all matching socket instances. If state is true, the underlying transport connection will be closed. This sends a DISCONNECT_SOCKETS document to all Socket.IO servers in the cluster.
func (*BroadcastOperator) Emit ¶
func (b *BroadcastOperator) Emit(ev string, args ...any) error
Emit broadcasts an event with the given name and arguments to all targeted clients. Returns an error if the event name is reserved or if broadcasting fails.
The message is sent by inserting a document into the MongoDB collection, matching the Node.js @socket.io/mongo-emitter wire protocol.
func (*BroadcastOperator) Except ¶
func (b *BroadcastOperator) Except(room ...socket.Room) BroadcastOperatorInterface
Except excludes one or more rooms from the broadcast. Returns a new BroadcastOperator with the rooms added to the exclusion list.
func (*BroadcastOperator) In ¶
func (b *BroadcastOperator) In(room ...socket.Room) BroadcastOperatorInterface
In is an alias for To, targeting one or more rooms for the broadcast.
func (*BroadcastOperator) ServerSideEmit ¶
func (b *BroadcastOperator) ServerSideEmit(args ...any) error
ServerSideEmit sends a message to all Socket.IO servers in the cluster. The first argument should be the event name, followed by any data arguments. Note: Acknowledgements are not supported when using the emitter.
func (*BroadcastOperator) SocketsJoin ¶
func (b *BroadcastOperator) SocketsJoin(rooms ...socket.Room) error
SocketsJoin makes all matching socket instances join the specified rooms. This sends a SOCKETS_JOIN document to all Socket.IO servers in the cluster.
func (*BroadcastOperator) SocketsLeave ¶
func (b *BroadcastOperator) SocketsLeave(rooms ...socket.Room) error
SocketsLeave makes all matching socket instances leave the specified rooms. This sends a SOCKETS_LEAVE document to all Socket.IO servers in the cluster.
func (*BroadcastOperator) To ¶
func (b *BroadcastOperator) To(room ...socket.Room) BroadcastOperatorInterface
To targets one or more rooms for the broadcast. Returns a new BroadcastOperator with the additional rooms included.
func (*BroadcastOperator) Volatile ¶
func (b *BroadcastOperator) Volatile() BroadcastOperatorInterface
Volatile sets the volatile flag for the broadcast. When set, the event data may be lost if the client is not ready to receive.
type BroadcastOperatorInterface ¶
type BroadcastOperatorInterface interface {
To(room ...socket.Room) BroadcastOperatorInterface
In(room ...socket.Room) BroadcastOperatorInterface
Except(room ...socket.Room) BroadcastOperatorInterface
Compress(compress bool) BroadcastOperatorInterface
Volatile() BroadcastOperatorInterface
Emit(ev string, args ...any) error
SocketsJoin(rooms ...socket.Room) error
SocketsLeave(rooms ...socket.Room) error
DisconnectSockets(state bool) error
ServerSideEmit(args ...any) error
}
BroadcastOperatorInterface defines the common interface for broadcast operators.
type BroadcastOptions ¶
type BroadcastOptions struct {
// Nsp is the Socket.IO namespace for the broadcast.
Nsp string
// AddCreatedAtField indicates whether to add a createdAt field to documents.
AddCreatedAtField bool
}
BroadcastOptions contains configuration for broadcasting messages via MongoDB.
type ClusterMessage ¶
type ClusterMessage = adapter.ClusterMessage
ClusterMessage is an alias for adapter.ClusterMessage. It is used for cluster communication.
type DisconnectSocketsMessage ¶
type DisconnectSocketsMessage = adapter.DisconnectSocketsMessage
DisconnectSocketsMessage is an alias for adapter.DisconnectSocketsMessage. It is used for disconnection operations.
type Emitter ¶
type Emitter struct {
// contains filtered or unexported fields
}
Emitter broadcasts messages to Socket.IO servers using MongoDB. It allows sending events to clients without running a full Socket.IO server.
func MakeEmitter ¶
func MakeEmitter() *Emitter
MakeEmitter creates a new Emitter with default options and the root namespace. Call Construct() to complete initialization before use.
func NewEmitter ¶
func NewEmitter(client *mongo.MongoClient, opts *EmitterOptions, nsps ...string) *Emitter
NewEmitter creates and initializes a new Emitter with the given MongoDB client and options. An optional namespace can be provided; if not specified, the root namespace "/" is used.
func (*Emitter) Compress ¶
func (e *Emitter) Compress(compress bool) BroadcastOperatorInterface
Compress sets the compress flag for the broadcast. When true, the message will be compressed before sending.
func (*Emitter) Construct ¶
func (e *Emitter) Construct(client *mongo.MongoClient, opts *EmitterOptions, nsps ...string)
Construct initializes the Emitter with the given MongoDB client, options, and namespace.
func (*Emitter) DisconnectSockets ¶
DisconnectSockets disconnects all matching socket instances. If state is true, the underlying connection will be closed.
func (*Emitter) Emit ¶
Emit broadcasts an event to all clients in the namespace. Returns an error if the event emission fails.
func (*Emitter) Except ¶
func (e *Emitter) Except(rooms ...socket.Room) BroadcastOperatorInterface
Except excludes specific room(s) from event emission. Returns a BroadcastOperatorInterface for method chaining.
func (*Emitter) In ¶
func (e *Emitter) In(rooms ...socket.Room) BroadcastOperatorInterface
In is an alias for To, targeting specific room(s) for event emission.
func (*Emitter) Of ¶
Of returns a new Emitter for the specified namespace. If the namespace doesn't start with "/", it will be prepended.
func (*Emitter) ServerSideEmit ¶
ServerSideEmit sends a message to all Socket.IO servers in the cluster. Note: Acknowledgements are not supported when using the emitter.
func (*Emitter) SocketsJoin ¶
SocketsJoin makes all matching socket instances join the specified rooms. This sends a request to all Socket.IO servers in the cluster.
func (*Emitter) SocketsLeave ¶
SocketsLeave makes all matching socket instances leave the specified rooms. This sends a request to all Socket.IO servers in the cluster.
func (*Emitter) To ¶
func (e *Emitter) To(rooms ...socket.Room) BroadcastOperatorInterface
To targets specific room(s) for event emission. Returns a BroadcastOperatorInterface for method chaining.
func (*Emitter) Volatile ¶
func (e *Emitter) Volatile() BroadcastOperatorInterface
Volatile sets a flag indicating the event data may be lost if the client is not ready to receive messages (e.g., due to network issues).
type EmitterOptions ¶
type EmitterOptions struct {
// contains filtered or unexported fields
}
EmitterOptions holds configuration options for the MongoDB emitter. All fields are optional and will use default values if not explicitly set.
func DefaultEmitterOptions ¶
func DefaultEmitterOptions() *EmitterOptions
DefaultEmitterOptions creates a new EmitterOptions instance with default values.
func (*EmitterOptions) AddCreatedAtField ¶
func (o *EmitterOptions) AddCreatedAtField() bool
AddCreatedAtField returns the configured addCreatedAtField value, or false if not set.
func (*EmitterOptions) Assign ¶
func (o *EmitterOptions) Assign(data EmitterOptionsInterface) EmitterOptionsInterface
Assign copies non-nil option values from another EmitterOptionsInterface. This allows merging configuration from multiple sources.
func (*EmitterOptions) GetRawAddCreatedAtField ¶
func (o *EmitterOptions) GetRawAddCreatedAtField() types.Optional[bool]
GetRawAddCreatedAtField returns the raw Optional value for addCreatedAtField.
func (*EmitterOptions) SetAddCreatedAtField ¶
func (o *EmitterOptions) SetAddCreatedAtField(v bool)
SetAddCreatedAtField sets whether to add a createdAt field to documents.
type EmitterOptionsInterface ¶
type EmitterOptionsInterface interface {
// SetAddCreatedAtField sets whether to add a createdAt field to documents.
SetAddCreatedAtField(bool)
// GetRawAddCreatedAtField returns the raw Optional wrapper for the addCreatedAtField setting.
GetRawAddCreatedAtField() types.Optional[bool]
// AddCreatedAtField returns the addCreatedAtField value, or false if not set.
AddCreatedAtField() bool
}
EmitterOptionsInterface defines the interface for configuring emitter options. It provides getters and setters for all configurable options.
type ServerSideEmitMessage ¶
type ServerSideEmitMessage = adapter.ServerSideEmitMessage
ServerSideEmitMessage is an alias for adapter.ServerSideEmitMessage. It is used for server-side emit operations.
type SocketsJoinLeaveMessage ¶
type SocketsJoinLeaveMessage = adapter.SocketsJoinLeaveMessage
SocketsJoinLeaveMessage is an alias for adapter.SocketsJoinLeaveMessage. It is used for join/leave operations.