Channels

gox.Channels provides access to the global register of active WebSocket channels.

gox
channel, exists := gox.Channels.Get(channelID)
if !exists {
    return
}

The record contains the active instances at that time, not the definitions declared within the directory websocket/.

When the cluster is enabled, it includes local channels and those maintained by other bodies. gox-server.


Structure

text
Channels
{
    Get(channelID string) (*gox.Channel, bool)
    Has(channelID string) bool
    List() []*gox.Channel
    Count() int
}

gox.Channels manages the global record. The methods and behavior of the object gox.Channel returned belong to the WebSocket channel API.


Methods

MethodDescription
Get()It recovers an active channel using its identifier.
Has()It indicates if a channel is active.
List()Returns a snapshot of the active channels.
Count()Returns the number of active channels.

Get()

Firma:func (*ChannelRegistry) Get(channelID string) (*gox.Channel, bool)

Get() it recovers an active instance by means of its complete identifier.

gox
channel, exists := gox.Channels.Get("Chat#general")
if !exists {
    return
}

When the identifier is empty, does not exist or belongs to a channel that has already been closed, it returns nil and false.

The recovered value is a normal instance of gox.Channel:

gox
channel, exists := gox.Channels.Get(channelID)
if !exists {
    return
}

channel.Emit("updated", "George")

The subsequent operations, such as consulting their identity, managing members, issuing events, removing sockets or closing the instance, belong to gox.Channelnot a gox.Channels.

To know the statement, the dynamic instances, the events, the membership and the life cycle of gox.Channel, visit the documentation of WebSocket Channels.

The members of a channel are represented by SocketCollection. See your reference for the available operations on the collection.


Has()

Firma:func (*ChannelRegistry) Has(channelID string) bool

Has() indicates whether an instance is active at the time of the query.

gox
if gox.Channels.Has("Chat#general") {
    log.Println("Canal activo")
}

An empty or unknown identifier returns false.

The result represents only the time of the query. The channel can be closed immediately after.

When you need to use the instance, call directly to Get() to avoid a separate check and search:

gox
channel, exists := gox.Channels.Get(channelID)
if !exists {
    return
}

channel.Emit("updated")

List()

Firma:func (*ChannelRegistry) List() []*gox.Channel

List() returns a snapshot of all active instances known to the registry.

gox
channels := gox.Channels.List()

for _, channel := range channels {
    log.Printf("Canal activo: %s", channel.ID())
}

The returned slice is independent of the record. Adding, removing or reordering its elements does not change the registered channels.

Each element continues to represent an active instance. A channel can be closed after creating the snapshot, so keeping the pointer does not guarantee that it remains available.

List() does not return values nil.


Count()

Firma:func (*ChannelRegistry) Count() int

Count() returns the number of active instances known to the registry.

gox
activeChannels := gox.Channels.Count()

The result represents the status of the record at the time of the query and can change immediately when a channel is created or closed.

Count() it accounts for instances, not definitions. Chat#general and Chat#support represent two active channels even if both use the same definition Chat.


Active instances

gox.Channels contains only channels that are active.

A definition declared in the directory websocket/ it does not appear in the register until the runtime creates an instance. Similarly, an instance is no longer available after it is closed.

gox
channel, exists := gox.Channels.Get("Chat#general")

The identifier must correspond to the complete instance. In a dynamic channel includes the name and key separated by #.

text
Chat#general

The existence of the definition Chat does not imply that Chat#general be active.


Distributed scope

Without a cluster, gox.Channels contains only the active channels of the current instance.

When the cluster is enabled, the record represents the active channels known to all the instances of gox-server.

gox
channel, exists := gox.Channels.Get(channelID)
if !exists {
    return
}

channel.Emit("updated")

The application does not need to check whether the channel is local or remote. Operations carried out on the recovered object are automatically distributed when necessary.

The snapshots produced by List() may contain local and remote representations.

To learn how to distribute channels between various instances, visit Cluster configuration.


Integration with Go

Get() uses the usual pattern value, ok of Go to distinguish a valid instance from an unknown identifier. List() returns a pointer slide to gox.Channelwhile Count() and Has() produce immediate values of the record.

The returned slices are independent snapshots, but the contained channels remain living resources. This behavior allows you to consult the record without exposing its internal structure and maintains a family API for any developer with go experience.


Best practices

  • Use Get() when you know the full channel identifier.
  • Preference Get() to combine Has() with a second search.
  • Do not confuse the active instances with the definitions declared in websocket/.
  • Do not assume that a channel obtained by Get() or List() remains active indefinitely.
  • Use List() only when you need to consult all active instances.
  • Do not modify the slice returned by List() hoping to alter the record.
  • It allows GOX to automatically solve the local or remote location of the channel.
  • Check the API of gox.Channel before administering the recovered instance.

See also

Next step

Continue with gox.Cluster to know the active distributed adapter and the management of your errors.

Cluster