Home

Awesome

<div align="center"> <img src="https://raw.githubusercontent.com/ryo-ma/deno-websocket/master/.assets/logo.png" width="200" alt="logo"/> </div>

deno websocket

deno doc GitHub nest badge

🦕 A simple WebSocket library like ws of node.js library for deno

This library is wrapping the ws standard library as a server-side and the native WebSocket API as a client-side. You can receive callbacks at the EventEmitter and can use the same object format on both the server-side and the client-side.

Quick Start

Example Demo

demo

Server side

$ deno run --allow-net https://deno.land/x/websocket@v0.1.4/example/server.ts

Client side

$ deno run --allow-net https://deno.land/x/websocket@v0.1.4/example/client.ts
ws connected! (type 'close' to quit)
> something

Usage

Server side

import { WebSocketClient, WebSocketServer } from "https://deno.land/x/websocket@v0.1.4/mod.ts";

const wss = new WebSocketServer(8080);
wss.on("connection", function (ws: WebSocketClient) {
  ws.on("message", function (message: string) {
    console.log(message);
    ws.send(message);
  });
});

Client side

import { WebSocketClient, StandardWebSocketClient } from "https://deno.land/x/websocket@v0.1.4/mod.ts";
const endpoint = "ws://127.0.0.1:8080";
const ws: WebSocketClient = new StandardWebSocketClient(endpoint);
ws.on("open", function() {
  console.log("ws connected!");
  ws.send("something");
});
ws.on("message", function (message: string) {
  console.log(message);
});

Documentation

WebSocketServer

Event

eventdetail
connectionEmitted when the handshake is complete
errorEmitted when an error occurs

Field

fielddetailtype
server.clientsA set that stores all connected clientsSet<WebSocket>

Method

methoddetail
close()Close the server

WebSocketClient

Event

eventdetail
openEmitted when the connection is established
closeEmitted when the connection is closed
messageEmitted when a message is received from the server
pingEmitted when a ping is received from the server
pongEmitted when a pong is received from the server
errorEmitted when an error occurs

Field

fielddetailtype
websocket.isClosedGet the close flagBoolean | undefined

Method

methoddetail
send(message:string | Unit8Array)Send a message
ping(message:string | Unit8Array)Send the ping
close([code:int[, reason:string]])Close the connection with the server
forceClose()Forcibly close the connection with the server

LICENSE

MIT LICENSE