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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@codeforbreakfast/eventsourcing-websocket

v0.3.17

Published

Batteries-included WebSocket event sourcing package - Complete WebSocket transport with default protocol for rapid event sourcing development

Readme

@codeforbreakfast/eventsourcing-websocket

WebSocket transport layer setup for event sourcing clients.

Purpose

This package provides the WebSocket transport implementation for connecting to event sourcing servers. Use this package ONLY for creating your Effect Layer - all application logic should use @codeforbreakfast/eventsourcing-protocol.

Key principle: Your application code shouldn't know it's using WebSocket. This is a deployment detail configured once when setting up layers.

Installation

bun add @codeforbreakfast/eventsourcing-websocket
bun add @codeforbreakfast/eventsourcing-protocol
bun add @codeforbreakfast/eventsourcing-commands

Layer Setup

Recommended: Layer-based Setup

import { makeWebSocketProtocolLayer } from '@codeforbreakfast/eventsourcing-websocket';
import { sendWireCommand, subscribe } from '@codeforbreakfast/eventsourcing-protocol';
import { Effect, Stream, pipe } from 'effect';

interface WireCommand {
  id: string;
  target: string;
  name: string;
  payload: unknown;
}

const sendCommand = (command: WireCommand) =>
  pipe(
    sendWireCommand(command),
    Effect.flatMap(() => subscribe('user-123')),
    Effect.flatMap((events) =>
      Stream.runForEach(events, (event) => Effect.sync(() => console.log('Event:', event.type)))
    )
  );

const myApplication = sendCommand({
  id: crypto.randomUUID(),
  target: 'user-123',
  name: 'CreateUser',
  payload: { name: 'Alice', email: '[email protected]' },
});

const layer = makeWebSocketProtocolLayer('ws://localhost:8080');

await Effect.runPromise(pipe(myApplication, Effect.provide(layer), Effect.scoped));

To switch transports (e.g., to HTTP), you'd only change the layer - application code stays the same.

Convenience: Direct Connection

For quick prototypes or scripts, you can use the makeWebSocketProtocolLayer directly with Effect.scoped:

import { makeWebSocketProtocolLayer } from '@codeforbreakfast/eventsourcing-websocket';
import { sendWireCommand } from '@codeforbreakfast/eventsourcing-protocol';
import { Effect, pipe } from 'effect';

const sendCommand = pipe(
  sendWireCommand({
    id: crypto.randomUUID(),
    target: 'user-123',
    name: 'CreateUser',
    payload: { name: 'Alice' },
  }),
  Effect.provide(makeWebSocketProtocolLayer('ws://localhost:8080')),
  Effect.scoped
);

Effect.runPromise(sendCommand);

Note: The layer-based approach is preferred for production applications as it supports dependency injection and testing.

API Reference

makeWebSocketProtocolLayer

import { Layer, Scope } from 'effect';
import { Protocol } from '@codeforbreakfast/eventsourcing-protocol';
import { TransportError, ConnectionError } from '@codeforbreakfast/eventsourcing-transport';

declare const makeWebSocketProtocolLayer: (
  url: string
) => Layer.Layer<Protocol, TransportError | ConnectionError, Scope.Scope>;

Creates an Effect Layer that provides the Protocol service over WebSocket.

Parameters:

  • url: string - WebSocket server URL (e.g., ws://localhost:8080)

Returns: Layer that can be provided to your application

Example:

import { Effect, pipe } from 'effect';
import { makeWebSocketProtocolLayer } from '@codeforbreakfast/eventsourcing-websocket';

declare const myApp: Effect.Effect<void, never, never>;

const runApp = pipe(
  myApp,
  Effect.provide(makeWebSocketProtocolLayer('ws://localhost:8080')),
  Effect.scoped
);

Effect.runPromise(runApp);

connect

import { Effect, Scope } from 'effect';
import { Protocol } from '@codeforbreakfast/eventsourcing-protocol';
import { TransportError, ConnectionError } from '@codeforbreakfast/eventsourcing-transport';

declare const connect: (
  url: string
) => Effect.Effect<Protocol, TransportError | ConnectionError, Protocol | Scope.Scope>;

Convenience function that connects and returns the Protocol service directly.

Parameters:

  • url: string - WebSocket server URL

Returns: Effect providing Protocol service (must be run in scoped context)

Example:

import { Effect, pipe } from 'effect';
import { connect } from '@codeforbreakfast/eventsourcing-websocket';

const program = pipe(
  connect('ws://localhost:8080'),
  Effect.flatMap((protocol) => Effect.succeed(protocol)), // use protocol...
  Effect.scoped
);

Next Steps

Once you've set up your WebSocket layer, see the following packages for building your application:

Advanced: Low-Level Transport

If you need access to the raw WebSocket transport (for advanced use cases), see:

Contributing

This package is part of the @codeforbreakfast/eventsourcing monorepo. See the main repository for contribution guidelines.

License

MIT