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

msw-phoenix.channel-binding

v0.3.1

Published

[![npm version](https://badge.fury.io/js/msw-phoenix.channel-binding.svg)](https://badge.fury.io/js/msw-phoenix.channel-binding)

Readme

npm version

msw-phoenix.channel-binding

npm version License: MIT

Phoenix Channel binding for Mock Service Worker (MSW) and @mswjs/interceptors.

Motivation

This package provides a wrapper over WebSocket connections from MSW and @mswjs/interceptors that automatically handles Phoenix Channel's custom messaging protocol. It provides automatic encoding and decoding of messages, letting you work with Phoenix channels in your tests and mocks as you would in production.

Phoenix channels implement a custom messaging protocol where messages are encoded as arrays like [null,"49","phoenix","heartbeat",{}]. This library handles that encoding/decoding transparently, so you can work with structured JavaScript objects instead.

Installation

npm install msw-phoenix.channel-binding
# or
yarn add msw-phoenix.channel-binding
# or
pnpm add msw-phoenix.channel-binding

Peer Dependencies: This package requires msw ^3.0.0 as a peer dependency.

Usage

Basic Example with @mswjs/interceptors

import { WebSocketInterceptor } from '@mswjs/interceptors'
import { toPhoenixChannel } from 'msw-phoenix.channel-binding'

const interceptor = new WebSocketInterceptor()

interceptor.on('connection', (connection) => {
  const phoenix = toPhoenixChannel(connection)

  // Handle client-side channel subscriptions
  phoenix.client.channel("room:lobby", (channel) => {
    channel.on(
      "greeting",
      (_event, { payload }: PhoenixChannelMessage<{ text: string }>) => {
        console.log(payload.text) // "Hello, John!"
      },
    );
  });
})

Using with Mock Service Worker (MSW)

import { ws } from 'msw'
import { setupServer } from 'msw/node'
import { toPhoenixChannel } from 'msw-phoenix.channel-binding'

const chat = ws.link('ws://localhost:4000/socket/websocket')

const server = setupServer(
  chat.addEventListener('connection', ({ client, server }) => {
    const phoenix = toPhoenixChannel({ client, server })
    
    // Set up channel handling
    phoenix.client.channel("room:*", (channel) => {
      // Handle join
      channel.onJoin = (topic, payload) => {
        console.log(`User joined ${topic}`, payload)
        return "ok"
      }
      
      // Handle messages
      channel.on("new_msg", (event, message) => {
        console.log('New message:', message.payload)
        
        // Broadcast to channel
        channel.push("new_msg", {
          user: "server",
          body: "Message received!"
        })
      })
      
      // Handle leave
      channel.onLeave = (topic) => {
        console.log(`User left ${topic}`)
        return "ok"
      }
    })
  })
)

server.listen()

Channel Wildcard Matching

You can use wildcards to match multiple channels:

// Matches any room channel: room:lobby, room:general, etc.
phoenix.client.channel("room:*", (channel) => {
  // Handle any room channel
})

// Exact match only
phoenix.client.channel("room:lobby", (channel) => {
  // Handle only room:lobby
})

Sending and Replying to Messages

phoenix.client.channel("room:lobby", (channel) => {
  // Push a message to the channel
  channel.push("new_msg", {
    user: "bot",
    body: "Hello everyone!"
  })
  
  // Reply to a specific message
  channel.on("ping", (event, message) => {
    if (message.ref) {
      channel.reply(message.ref, {
        status: "ok",
        response: { pong: true }
      })
    }
  })
})

Using MockPresence

Test presence functionality without a real Phoenix server:

import { MockPresence } from 'msw-phoenix.channel-binding'

const presence = new MockPresence()

phoenix.client.channel("room:lobby", (channel) => {
  // Subscribe the channel to presence updates
  presence.subscribe(channel)
  
  // Track a user
  const tracker = presence.track("user:123", {
    name: "John Doe",
    online_at: Date.now()
  })
  
  // List all presences
  console.log(presence.list())
  // { "user:123": { metas: [{ name: "John Doe", online_at: 1234567890, phx_ref: "..." }] } }
  
  // Update presence meta
  tracker.update({
    name: "John Doe",
    status: "away",
    online_at: Date.now()
  })
  
  // Untrack when done
  tracker.untrack()
})

License

MIT - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Links


This package works with both Mock Service Worker and @mswjs/interceptors for WebSocket testing.