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

next-ws

v2.0.0

Published

Add support for WebSockets in Next.js 13 app directory

Downloads

8,800

Readme

🤔 About

next-ws is a simple package that adds WebSocket support to your Next.js app directory. With next-ws, you no longer need to create a separate WebSocket server to handle WebSocket connections. Instead, you can handle WebSocket connections directly in your Next.js API routes.

[!IMPORTANT]
Next WS is designed for use in server-based environments. It is not suitable for serverless platforms like Vercel, where WebSocket servers are not supported. Furthermore, this plugin is built for the app directory and does not support the older pages directory.

🏓 Table of Contents

📦 Installation

To set up a WebSocket server with next-ws, you need to patch your local Next.js installation. next-ws simplifies this process by providing a CLI command that handles the patching for you. Follow these steps to get started:

  1. Install Dependencies: Use your preferred package manager to install next-ws and its peer dependency ws:

    npm install next-ws ws
    pnpm add next-ws ws
    yarn add next-ws ws
  2. Add Prepare Script: Add the following prepare script to your package.json. The prepare script is a lifecycle script that runs automatically when you run npm install, ensuring that your Next.js installation is patched with next-ws every time you install it:

    {
      "scripts": {
        "prepare": "next-ws patch"
      }
    }

🚀 Usage

Using WebSocket connections in your Next.js app directory is simple with next-ws. You can handle WebSocket connections directly in your API routes via exported SOCKET functions. Here's an example of a simple WebSocket echo server:

export function SOCKET(
  client: import('ws').WebSocket,
  request: import('http').IncomingMessage,
  server: import('ws').WebSocketServer,
  context: { params: Record<string, string | string[]> },
) {
  // ...
}

🌀 Examples

[!TIP]
For more detailed examples, refer the examples directory.

Echo Server

This example demonstrates a simple WebSocket echo server that sends back any message it receives. Create a new API route file anywhere in your app directory and export a SOCKET function to handle WebSocket connections:

// app/api/ws/route.ts (can be any route file in the app directory)

export function SOCKET(
  client: import("ws").WebSocket,
  request: import("http").IncomingMessage,
  server: import("ws").WebSocketServer
) {
  console.log("A client connected");

  client.on("message", (message) => {
    console.log("Received message:", message);
    client.send(message);
  });

  client.on("close", () => {
    console.log("A client disconnected");
  });
}

You can now connect to your WebSocket server, send it a message and receive the same message back.

Chat Room

See the chat room example.