npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

effect-websocket-bun

v1.0.1

Published

Bun WebSocket server implementation for Effect-TS using Bun's native WebSocket API, providing fast and efficient WebSocket servers with Effect's functional programming model

Readme

Effect WebSocket Bun

CI npm version License: MIT Bun Version

Bun WebSocket server implementation for Effect-TS using Bun's native WebSocket API, providing fast and efficient WebSocket servers with Effect's functional programming model.

Overview

This package provides the Bun-specific implementation of the Effect WebSocket server interface. It leverages Bun's native WebSocket API for maximum performance and seamless integration with the Bun runtime.

Features

  • Native Performance: Built on Bun's native WebSocket implementation
  • Effect Integration: Full integration with Effect-TS error handling and effects
  • Streaming: Message and connection streams using Effect's Stream API
  • Type Safety: Full TypeScript support with comprehensive type definitions
  • Bun Optimized: Tailored for Bun's fast startup and runtime performance

Installation

bun add effect-websocket effect-websocket-bun @effect/platform effect

Quick Start

import { Effect, Stream } from "effect"
import { WebSocketServer } from "effect-websocket"
import { makeWebSocketServer } from "effect-websocket-bun"

const program = Effect.scoped(
  makeWebSocketServer({ port: 8080 }, (server) =>
    Effect.gen(function* () {
      console.log("WebSocket server started on port 8080")

      // Handle new connections
      yield* Stream.runForEach(server.connections, (connection) => {
        console.log("New connection:", connection.id)

        // Handle messages from this connection
        return Stream.runForEach(connection.messages, (message) => {
          console.log(`Message from ${connection.id}:`, message)

          // Echo the message back
          return connection.send(`Echo: ${message}`)
        })
      })

      yield* Effect.never
    })
  )
)

Effect.runPromise(program)

Server Options

interface WebSocketServerOptions {
  port?: number              // Server port (default: 8080)
  hostname?: string          // Server hostname (default: "localhost")
  tls?: {
    key: string            // TLS private key
    cert: string           // TLS certificate
  }
  maxPayloadLength?: number  // Maximum payload length in bytes
  backpressureLimit?: number // Backpressure limit
  closeOnBackpressureLimit?: boolean  // Close connection on backpressure limit
  perMessageDeflate?: boolean // Enable per-message deflate compression
  webSocket?: {
    message?: (ws: WebSocket, message: string | Buffer) => void
    open?: (ws: WebSocket) => void
    close?: (ws: WebSocket, code: number, reason: string) => void
    error?: (ws: WebSocket, error: Error) => void
  }
}

Advanced Usage

TLS/SSL Support

import { makeWebSocketServer } from "effect-websocket-bun"
import { readFileSync } from "fs"

const program = Effect.scoped(
  makeWebSocketServer({
    port: 443,
    tls: {
      key: readFileSync("path/to/private-key.pem", "utf8"),
      cert: readFileSync("path/to/certificate.pem", "utf8")
    }
  }, (server) => {
    // Secure WebSocket server (wss://)
    return Effect.gen(function* () {
      yield* Stream.runForEach(server.connections, (connection) => {
        return Stream.runForEach(connection.messages, (message) => {
          return connection.send(`Secure: ${message}`)
        })
      })

      yield* Effect.never
    })
  })
)

Backpressure Handling

const program = Effect.scoped(
  makeWebSocketServer({
    port: 8080,
    backpressureLimit: 1024 * 1024, // 1MB
    closeOnBackpressureLimit: true
  }, (server) => {
    // Server with backpressure management
    return Effect.gen(function* () {
      yield* Stream.runForEach(server.connections, (connection) => {
        return Stream.runForEach(connection.messages, (message) => {
          // Handle high-throughput scenarios
          return connection.send(`Processed: ${message}`)
        })
      })

      yield* Effect.never
    })
  })
)

API Reference

makeWebSocketServer

makeWebSocketServer(
  options: WebSocketServerOptions,
  f: (server: WebSocketServer) => Effect<never, never, void>
): Effect<Scope, WebSocketServerError, void>

Creates and manages a WebSocket server with automatic cleanup.

Parameters:

  • options: Server configuration options
  • f: Function that receives the server instance and returns an Effect

Returns: An Effect that manages the server lifecycle

Performance Features

Bun's native WebSocket implementation provides:

  • Fast Startup: Minimal cold start time
  • High Throughput: Optimized for high-concurrency scenarios
  • Low Latency: Native implementation reduces overhead
  • Memory Efficient: Built-in memory management optimizations

Error Handling

The package provides comprehensive error handling through Effect-TS:

import { WebSocketServerError } from "effect-websocket-bun"

const program = Effect.scoped(
  makeWebSocketServer({ port: 8080 }, (server) => {
    return Effect.catchAll(
      serverLogic(server),
      (error) => {
        if (error._tag === "WebSocketServerError") {
          console.error("Server error:", error.message)
          return Effect.fail(error)
        }
        return Effect.fail(error)
      }
    )
  })
)

Bun-Specific Optimizations

  • Native WebSocket API: Direct access to Bun's optimized WebSocket implementation
  • Fast Message Processing: Optimized for Bun's event loop
  • Memory Management: Automatic memory management for high-throughput scenarios
  • Type Safety: Full TypeScript integration with Bun's type definitions

Documentation

Requirements

  • Bun: >= 1.0.0
  • TypeScript: >= 5.0.0
  • Effect: >= 3.17.14

License

MIT