Skip to main content
This package has been archived, and as such it is read-only.

Built and signed on GitHub Actions

[Moved to effectionx namespace] Use the WebSocket API as an Effection resource.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 months ago (2.0.0)

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

WebSocket

A streamlined WebSocket client for Effection programs that transforms the event-based WebSocket API into a clean, resource-oriented stream.

Why Use this API?

Traditional WebSocket API require managing multiple event handlers (open, close, error, message) which can become complex and error-prone.

This package simplifies WebSocket usage by:

  • Providing a clean stream-based interface
  • Handling connection state management automatically
  • Implementing proper error handling
  • Ensuring resource cleanup

Basic Usage

import { each, main } from "effection";
import { useWebSocket } from "@effection-contrib/websocket";

await main(function* () {
  // Connection is guaranteed to be open when this returns
  let socket = yield* useWebSocket("ws://websocket.example.org");

  // Send messages to the server
  socket.send("Hello World");

  // Receive messages using a simple iterator
  for (let message of yield* each(socket)) {
    console.log("Message from server", message);
    yield* each.next();
  }
});

Features

  • Ready-to-use Connections: useWebSocket() returns only after the connection is established
  • Automatic Error Handling: Socket errors are properly propagated to your error boundary
  • Stream-based API: Messages are delivered through a simple stream interface
  • Clean Resource Management: Connections are properly cleaned up when the operation completes

Advanced Usage

Custom WebSocket Implementations

For environments without native WebSocket support (like Node.js < 21), you can provide your own WebSocket implementation:

import { createWebSocket } from "my-websocket-client";
import { each, main } from "effection";
import { useWebSocket } from "@effection-contrib/websocket";

await main(function* () {
  let socket = yield* useWebSocket(() =>
    createWebSocket("ws://websocket.example.org")
  );

  for (let message of yield* each(socket)) {
    console.log("Message from server", message);
    yield* each.next();
  }
});
Built and signed on
GitHub Actions
View transparency log

Add Package

deno add jsr:@effection-contrib/websocket

Import symbol

import * as websocket from "@effection-contrib/websocket";

---- OR ----

Import directly with a jsr specifier

import * as websocket from "jsr:@effection-contrib/websocket";

Add Package

npx jsr add @effection-contrib/websocket

Import symbol

import * as websocket from "@effection-contrib/websocket";

Add Package

yarn dlx jsr add @effection-contrib/websocket

Import symbol

import * as websocket from "@effection-contrib/websocket";

Add Package

pnpm dlx jsr add @effection-contrib/websocket

Import symbol

import * as websocket from "@effection-contrib/websocket";

Add Package

bunx jsr add @effection-contrib/websocket

Import symbol

import * as websocket from "@effection-contrib/websocket";