Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/wocket/src/server.ts>Server

A WebSocket library for Deno
Latest
class Server
import { Server } from "https://deno.land/x/wocket@v1.0.0/src/server.ts";

A class to create a websocket server, handling clients connecting, and being able to handle messages from them, and send messages to them

Constructors

new
Server(options: IOptions)

Properties

readonly
address: string

Get the full address that this server is running on.

channels: Map<string, Channel>

A map of all created channels. The key is the channel name and the value is the channel object.

clients: Map<number, Client>

A map of all clients connected to this server. The key is the client's ID and the value is the client object.

Methods

broadcast(
channelName: string,
message: Record<string, unknown>,
id: number,
)

Broadcast to other clients in a channel excluding the one passed in

close(): Promise<void>

Close the server, stopping all resources and breaking all connections to clients

on<CustomProps extends Record<string, unknown> | string | Uint8Array, ChannelName extends string = any>(channelName: ChannelName, cb: OnChannelCallback<CustomProps, ChannelName>): void

Do the following:

1. Create a channel (if it does not already exist).
2. Add a callback to that channel. This callback will be executed when
   events are sent to the channel.
3. Store the callback in the list of callbacks that the channel has.
run(): void

Run the sever.

to(
channelName: string,
message: Record<string, unknown> | string,
onlySendTo?: number,
): void

Send a message to the channel, so clients listening on that channel will receive this message