Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/sockets/src/event_emitter.ts>EventEmitter

A WebSocket library for Deno.
Latest
class EventEmitter
import { EventEmitter } from "https://deno.land/x/sockets@v0.5.0/src/event_emitter.ts";

The EventEmitter class is responsible for the logic of sending and receiving messages. To do this, it aggregates information on clients, such as tracking how many clients are connected and what channels are open.

Constructors

new
EventEmitter()

Construct an object of this class.

Properties

channels: { [key: string]: Channel; }

A list of key value pairs describing all created channels, where the key is the channel name, and the value represents the channel object.

clients: { [key: number]: Client; }

A list of key value pairs describing all clients connected, where the key is the client id, and the value represents the client object.

id: string

Used to identify this class (when having sent messages) as the Server.

sender: Sender

Instance of the Sender class

Methods

private
queuePacket(packet: Packet): void

Add a packet to the queue so that the message contained in the packet can be sent to the client(s).

addClientToChannel(channelName: string, clientId: number): void

Adds a new client to a channel. Once the client is added, the client will be able to receive messages sent to the channel.

broadcast(channelName: string, message: unknown): void

Broadcasts a message to all receivers of a channel. pkgOrMessage does not contain "from" key.

closeChannel(channelName: string): void

Close a channel.

createClient(clientId: number, clientSocket: WebSocket): Client

Adds a new client.

getChannel(channelName: string): Channel

Get a channel by the channel name

getChannels(): string[]

Get all of the channels.

getClients(): { [key: string]: Client; }

Get all clients.

on(name: string, cb: Function): void

This is the same as creating a new channel (openChannel()), but for internal use.

openChannel(channelName: string): void

Create a new channel. Basically, this creates a new event that clients can listen to. Ther server can also send messages to this new event/channel.

removeClient(clientId: number): void

Removes an existing client from server and any channels that the client subscribed to.

removeClientFromChannel(channelName: string, clientId: number): void

Removes a client from a channel.

to(channelName: string, message: unknown): void

Send a message to a channel, excluding the sender.