{
 "kind": "tool",
 "slug": "ws",
 "path": "tools/ws/",
 "name": "ws",
 "maker": null,
 "density": "full",
 "crumbs": [
  [
   "Tools",
   "tools/"
  ],
  [
   "HTTP clients and networking",
   "tools/http-clients/"
  ]
 ],
 "lead": "Live two-way connections for Node.js servers.",
 "chips": [
  {
   "label": "HTTP clients and networking",
   "path": "tools/http-clients/"
  },
  {
   "label": "NPM"
  }
 ],
 "primary": {
  "label": "Copy install command",
  "copy": "npm install ws"
 },
 "links": [
  {
   "label": "GitHub",
   "href": "https://github.com/websockets/ws"
  },
  {
   "label": "Website",
   "href": "https://github.com/websockets/ws"
  }
 ],
 "track": "New versions, deprecation and license changes on your dashboard",
 "verdicts": [
  {
   "key": "license",
   "label": "License",
   "value": "MIT",
   "status": "yes",
   "sub": "Use it in paid apps",
   "quote": "Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction",
   "note": "MIT allows commercial use; keep the copyright and license notice with the code.",
   "src": "https://github.com/websockets/ws/blob/master/LICENSE"
  },
  {
   "key": "maint",
   "label": "Maintained",
   "value": "Maintained",
   "status": "yes",
   "sub": "Last change Nov 16, 2026",
   "note": "Latest GitHub release Nov 12, 2026. ",
   "src": "https://github.com/websockets/ws"
  },
  {
   "key": "pop",
   "label": "Used by agents' projects",
   "value": "204 projects",
   "status": "info",
   "sub": "#4 of 126 in its category",
   "note": "Dependency in 204 public GitHub projects set up for AI coding agents that we scanned (3.3%).",
   "src": null
  },
  {
   "key": "agent",
   "label": "Agent access",
   "value": "Install only",
   "status": "info",
   "sub": null,
   "note": "Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.",
   "src": null
  }
 ],
 "vfoot": "From the package registry and GitHub, checked daily; card checked Nov 10, 2026.",
 "blocks": [
  {
   "id": "overview",
   "nav": "Overview",
   "h2": "What ws does",
   "parts": [
    {
     "t": "prose",
     "text": "ws is a free library for Node.js that keeps a live, two-way connection open between your server and each visitor. The server can push new chat messages, prices or progress updates the moment they happen, without the page asking again. It runs only on the server side; the browser uses its own built-in WebSocket."
    },
    {
     "t": "h3",
     "text": "What people build with it"
    },
    {
     "t": "uses",
     "items": [
      {
       "title": "Live chat or comments",
       "text": "A message one visitor sends shows up for everyone else on the page at once, without reloading.",
       "idea": false
      },
      {
       "title": "Push updates from your server",
       "text": "Your Node.js server sends new orders, scores or job progress to the open page the moment they change.",
       "idea": false
      },
      {
       "title": "Listen to a live data feed",
       "text": "A backend script connects to another service's live feed, such as market prices or events, and reacts to each message.",
       "idea": false
      },
      {
       "title": "Let only signed-in users connect",
       "text": "Check the visitor's login before the live connection opens, and turn strangers away with \"401 Unauthorized\".",
       "idea": false
      },
      {
       "title": "Close dead connections",
       "text": "Find visitors whose phone lost signal or whose laptop went to sleep, and free the server's memory for them.",
       "idea": false
      },
      {
       "title": "Show an AI agent's progress live",
       "text": "While a long task runs on your server, send each step to the page so the person sees it moving instead of a spinner.",
       "idea": true
      }
     ]
    }
   ]
  },
  {
   "id": "popularity",
   "nav": "Popularity",
   "h2": "ws popularity",
   "parts": [
    {
     "t": "stats",
     "items": [
      {
       "n": "82",
       "of": "/ 100",
       "l": "Gid Score, #64 overall"
      },
      {
       "n": "427M",
       "l": "downloads a week, npm"
      },
      {
       "n": "22.8K",
       "l": "GitHub stars"
      },
      {
       "n": "204",
       "l": "agent projects use it"
      }
     ]
    },
    {
     "t": "small",
     "text": "Gid Score measures public popularity and activity, not quality.",
     "link": "How the Gid Score is calculated",
     "path": "tools/top/"
    }
   ]
  },
  {
   "id": "usage",
   "nav": "How it is used",
   "h2": "How ws is used",
   "parts": [
    {
     "t": "list",
     "items": [
      "ws is a dependency in 204 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (4.2% of the 4,867 JavaScript/TypeScript ones), #4 of 126 in http clients and networking.",
      "Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 902; those are left out of every share on this page.",
      "The most used alternative in our scan is Socket.IO Client (69 projects).",
      "Projects that use it most often also use tsx (57.4%), dotenv (52.5%), Zod (64.7%).",
      "It is more common in projects set up for OpenClaw (16.5%) than for Cursor (2.7%)."
     ]
    },
    {
     "t": "h3",
     "text": "By coding agent"
    },
    {
     "t": "rows",
     "rows": [
      {
       "l": "144 projects, 4.4%",
       "v": "Claude Code",
       "path": "agents/claude-code/"
      },
      {
       "l": "89 projects, 2.9%",
       "v": "GitHub Copilot",
       "path": "agents/github-copilot/"
      },
      {
       "l": "66 projects, 2.7%",
       "v": "Cursor",
       "path": "agents/cursor/"
      },
      {
       "l": "55 projects, 16.5%",
       "v": "OpenClaw",
       "path": "agents/openclaw/"
      },
      {
       "l": "33 projects, 7.0%",
       "v": "Gemini CLI",
       "path": "agents/gemini-cli/"
      },
      {
       "l": "17 projects, 3.6%",
       "v": "OpenAI Codex",
       "path": "agents/openai-codex/"
      },
      {
       "l": "15 projects, 5.4%",
       "v": "Devin Desktop",
       "path": "agents/devin-desktop/"
      },
      {
       "l": "14 projects, 4.1%",
       "v": "OpenCode",
       "path": "agents/opencode/"
      }
     ]
    },
    {
     "t": "h3",
     "text": "Often used with"
    },
    {
     "t": "rows",
     "rows": [
      {
       "l": "In 57.4% of its projects",
       "v": "tsx",
       "path": "tools/tsx/"
      },
      {
       "l": "In 52.5% of its projects",
       "v": "dotenv",
       "path": "tools/dotenv/"
      },
      {
       "l": "In 64.7% of its projects",
       "v": "Zod",
       "path": "tools/zod/"
      },
      {
       "l": "In 41.2% of its projects",
       "v": "Express",
       "path": "tools/express/"
      },
      {
       "l": "In 58.3% of its projects",
       "v": "Vitest",
       "path": "tools/vitest/"
      },
      {
       "l": "In 85.8% of its projects",
       "v": "TypeScript",
       "path": "tools/typescript/"
      },
      {
       "l": "In 32.8% of its projects",
       "v": "Commander",
       "path": "tools/commander/"
      },
      {
       "l": "In 31.9% of its projects",
       "v": "chalk",
       "path": "tools/chalk/"
      }
     ]
    }
   ]
  },
  {
   "id": "profile",
   "nav": "Profile",
   "h2": "ws in practice",
   "parts": [
    {
     "t": "h3",
     "text": "Strengths"
    },
    {
     "t": "rows",
     "rows": [
      {
       "l": "Autobahn reports for server and client",
       "v": "The README says ws passes the \"quite extensive\" Autobahn test suite. It links one published report for the server and a separate one for the client."
      },
      {
       "l": "Several endpoints on one HTTP server",
       "v": "With <code>noServer: true</code>, the HTTP server's <code>upgrade</code> event picks which WebSocket server takes each connection. In the README this is done by pathname, and anything unmatched gets <code>socket.destroy()</code>."
      },
      {
       "l": "Reject before the handshake",
       "v": "The README's client-authentication example checks the request inside the <code>upgrade</code> handler. On failure it writes a 401 and destroys the socket. Only authenticated requests reach <code>handleUpgrade</code>, and the authenticated client is passed along to the <code>connection</code> event."
      },
      {
       "l": "Opt-in native addon",
       "v": "<code>bufferutil</code> is an optional module that can be installed alongside ws. The README says it improves the performance of certain operations, such as masking and unmasking frame payloads. It also says prebuilt binaries mean you don't necessarily need a C++ compiler."
      }
     ]
    },
    {
     "t": "h3",
     "text": "Watch out for"
    },
    {
     "t": "rows",
     "rows": [
      {
       "l": "Node.js only",
       "v": "Browser clients must use the native <code>WebSocket</code>. For code that runs in both places, the README points to wrappers on npm such as <code>isomorphic-ws</code>."
      },
      {
       "l": "Different compression defaults per side",
       "v": "permessage-deflate is off by default on the server and on by default on the client. The README says it adds \"significant overhead\", and the maintainers write that they suggest enabling it \"only if it is really needed\"."
      },
      {
       "l": "zlib under concurrency",
       "v": "The README warns that Node.js compression can lead to \"catastrophic memory fragmentation\" and slow performance with more concurrency, especially on Linux. It advises a workload-representative test before using compression in production."
      }
     ]
    },
    {
     "t": "h3",
     "text": "Simple server"
    },
    {
     "t": "tabs",
     "label": "Example",
     "items": [
      {
       "tab": "Js",
       "code": "import { WebSocketServer } from 'ws';\n\nconst wss = new WebSocketServer({ port: 8080 });\n\nwss.on('connection', function connection(ws) {\n  ws.on('error', console.error);\n\n  ws.on('message', function message(data) {\n    console.log('received: %s', data);\n  });\n\n  ws.send('something');\n});"
      }
     ]
    }
   ]
  },
  {
   "id": "more",
   "nav": null,
   "spy_as": "profile",
   "h2": "More about ws",
   "parts": [
    {
     "t": "more",
     "items": [
      {
       "title": "Funding",
       "path": "tools/funding/",
       "items": [
        "GitHub Sponsors: https://github.com/lpinca",
        "7 GitHub sponsors"
       ]
      },
      {
       "title": "Head to head",
       "items": [
        {
         "text": "ws vs Socket.IO Client",
         "note": "used together in 10 projects",
         "path": "tools/compare/ws-vs-socketio-client/"
        },
        {
         "text": "ws vs Socket.IO",
         "note": "used together in 9 projects",
         "path": "tools/compare/ws-vs-socketio/"
        }
       ]
      },
      {
       "title": "Packages around it",
       "items": [
        "tsx: TypeScript runner",
        "express: Web framework",
        "typescript: Typed JavaScript",
        "zod: Schema validation",
        "dotenv: Env file loader",
        "vitest: Test runner"
       ]
      },
      {
       "title": "More in HTTP clients and networking",
       "path": "tools/http-clients/",
       "items": [
        {
         "text": "Axios",
         "path": "tools/axios/"
        },
        {
         "text": "Requests",
         "path": "tools/requests-py/"
        },
        {
         "text": "httpx",
         "path": "tools/httpx/"
        },
        {
         "text": "postgres",
         "path": "tools/postgres/"
        },
        {
         "text": "aiohttp",
         "path": "tools/aiohttp/"
        },
        {
         "text": "websockets",
         "path": "tools/websockets/"
        },
        {
         "text": "redis (Python)",
         "path": "tools/redis-pypi/"
        },
        {
         "text": "undici",
         "path": "tools/undici/"
        }
       ]
      }
     ]
    }
   ]
  },
  {
   "id": "alternatives",
   "nav": "Alternatives",
   "h2": "Alternatives",
   "parts": [
    {
     "t": "alts",
     "items": [
      {
       "name": "ws",
       "me": true,
       "facts": [
        "MIT",
        "Gid 82",
        "427M/wk"
       ]
      },
      {
       "name": "Socket.IO",
       "why": "Pick Socket.IO when you want rooms, automatic reconnection and fallback transports built in, and can use its own client library.",
       "path": "tools/socket-io/",
       "facts": [
        "MIT",
        "Gid 79",
        "26M/wk"
       ]
      },
      {
       "name": "websockets",
       "why": "Pick websockets when your server is written in Python instead of Node.js.",
       "path": "tools/websockets/",
       "facts": []
      },
      {
       "name": "aiohttp",
       "why": "Pick aiohttp when you want one Python async framework for both web pages and WebSocket connections.",
       "path": "tools/aiohttp/",
       "facts": []
      },
      {
       "name": "Socket.IO Client",
       "why": "Real-time bidirectional communication library for JavaScript with fallbacks.",
       "path": "tools/socket-io-client/",
       "facts": [
        "MIT",
        "Gid 80",
        "22.3M/wk"
       ]
      }
     ],
     "more": "Compare ws with another tool",
     "more_path": "tools/compare/"
    }
   ]
  },
  {
   "id": "start",
   "nav": "Get started",
   "h2": "Get started",
   "parts": [
    {
     "t": "prompt",
     "text": "Use the npm package ws (pin ws@8.22.2, Node.js) for the WebSocket server; import { WebSocketServer } from 'ws', check the login in the HTTP server's 'upgrade' event, set maxPayload, and use the browser's native WebSocket on the client."
    },
    {
     "t": "steps",
     "items": [
      {
       "text": "Add ws to your Node.js project.",
       "code": "npm install ws",
       "src": "https://github.com/websockets/ws/blob/master/README.md"
      },
      {
       "text": "Start a server that greets each new connection and logs what it receives.",
       "code": "import { WebSocketServer } from 'ws';\nconst wss = new WebSocketServer({ port: 8080 });\nwss.on('connection', function connection(ws) {\n  ws.on('error', console.error);\n  ws.on('message', function message(data) {\n    console.log('received: %s', data);\n  });\n  ws.send('something');\n});\n",
       "src": "https://github.com/websockets/ws/blob/master/README.md"
      },
      {
       "text": "In the browser, connect with the built-in WebSocket object, not with ws.",
       "code": null,
       "src": "https://github.com/websockets/ws/blob/master/README.md"
      }
     ]
    },
    {
     "t": "h3",
     "text": "Rules for your agent"
    },
    {
     "t": "rules",
     "label": "Paste into AGENTS.md or CLAUDE.md",
     "text": "## ws\n- Use ws only in Node.js code. Browser code must use the native `WebSocket` object.\n- Register an `error` listener on each connection, as the README examples do (`ws.on('error', console.error)`).\n- To serve several WebSocket endpoints or to authenticate on one HTTP server, create servers with `noServer: true`. Route or authenticate in the HTTP server's `upgrade` handler, then call `handleUpgrade` and emit `connection`.\n- When broadcasting, send only to clients whose `readyState === WebSocket.OPEN`, and pass `{ binary: isBinary }` through.\n- Keep `perMessageDeflate` at its server default (disabled) and enable it only if it is really needed. If you enable it, test memory and performance under a representative workload first.\n- For broken-connection detection, follow the README heartbeat: mark clients alive on `pong`, `terminate()` ones that did not answer, and clear the interval on server `close`.\n- Behind a proxy like NGINX, the README gives the `X-Forwarded-For` header as the de-facto standard for the client IP.\n"
    },
    {
     "t": "h3",
     "text": "Mistakes to avoid"
    },
    {
     "t": "mistakes",
     "items": [
      {
       "dont": "import WebSocket from 'ws'; new WebSocket.Server({ port: 8080 }) (from old CommonJS tutorials)",
       "fix": "import { WebSocketServer } from 'ws'; new WebSocketServer({ port: 8080 })",
       "why": "WebSocket.Server is attached only in the CommonJS entry index.js; the ES module wrapper exports WebSocketServer as a named export and does not load index.js."
      },
      {
       "dont": "JSON.parse(data) or data === 'ping' in the 'message' handler, as in ws 7 code",
       "fix": "const text = isBinary ? data : data.toString(); then JSON.parse(text)",
       "why": "Since 8.0.0 text messages arrive as Buffers and the handler gets an isBinary flag."
      },
      {
       "dont": "import WebSocket from 'ws' in front-end code (React, Vue, plain pages)",
       "fix": "Use the browser's global WebSocket; keep ws on the server",
       "why": "ws is for Node.js only; its browser entry (browser.js) just throws an error telling you to use the native WebSocket."
      },
      {
       "dont": "Checking tokens with the verifyClient option",
       "fix": "Authenticate in server.on('upgrade') and call wss.handleUpgrade only for valid users (noServer mode)",
       "why": "The docs discourage verifyClient and show the upgrade-event pattern instead."
      },
      {
       "dont": "Leaving maxPayload at its default and pinning an old 8.x release",
       "fix": "Set maxPayload to what your messages need (for example 64 * 1024) and use 8.21.0 or newer",
       "why": "The default allows 100 MiB per message, and 8.20.1 and 8.21.0 fixed a memory disclosure bug and a memory exhaustion attack."
      }
     ]
    }
   ]
  },
  {
   "id": "faq",
   "nav": "Questions",
   "h2": "Questions",
   "parts": [
    {
     "t": "faq",
     "items": [
      {
       "q": "Can I use ws in a React or plain browser page?",
       "a": "No. In the browser use the built-in WebSocket object; ws is for Node.js servers and backend clients.",
       "evidence": {
        "url": "https://github.com/websockets/ws/blob/master/README.md",
        "quote": "Browser clients must use the native WebSocket object."
       }
      },
      {
       "q": "How do I keep strangers out?",
       "a": "Check the login in the HTTP server's 'upgrade' event and only then hand the connection to ws; the verifyClient option is discouraged.",
       "evidence": {
        "url": "https://github.com/websockets/ws/blob/master/doc/ws.md",
        "quote": "Use of verifyClient is discouraged."
       }
      },
      {
       "q": "How do I notice that a visitor is gone?",
       "a": "Send pings on a timer and terminate connections that did not answer with a pong since the last round.",
       "evidence": {
        "url": "https://github.com/websockets/ws/blob/master/README.md",
        "quote": "In these cases, ping messages can be used as a means to verify that the remote endpoint is still responsive."
       }
      },
      {
       "q": "Can it share a port with my Express or HTTP server?",
       "a": "Yes. Pass your server as the server option, or use noServer mode and route the 'upgrade' event yourself.",
       "evidence": {
        "url": "https://github.com/websockets/ws/blob/master/doc/ws.md",
        "quote": "The \"noServer\" mode allows the WebSocket server to be completely detached from the HTTP/S server."
       }
      },
      {
       "q": "Can I use it in a paid product?",
       "a": "Yes, it is under the MIT license.",
       "evidence": {
        "url": "https://github.com/websockets/ws/blob/master/LICENSE",
        "quote": "Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction"
       }
      }
     ]
    }
   ]
  }
 ],
 "glance": [
  [
   "Package",
   "npm ws"
  ],
  [
   "Category",
   "HTTP clients and networking"
  ],
  [
   "License",
   "MIT"
  ],
  [
   "Latest version",
   "8.22.0, September 26, 2026"
  ],
  [
   "On GitHub since",
   "November 2011"
  ],
  [
   "Gid Score",
   "82.4"
  ],
  [
   "Rank in category",
   "#4 of 126"
  ],
  [
   "GitHub",
   "websockets/ws"
  ],
  [
   "Last change",
   "Nov 16, 2026"
  ]
 ],
 "good": [
  "It does not run in the browser. Web pages use the browser's own built-in WebSocket to talk to a ws server.",
  "By default one message may be as large as 100 MiB. Set a smaller limit for chats and forms so one visitor cannot fill the server's memory.",
  "Compression of messages is off on the server by default. The docs advise turning it on only when you really need it, because it costs speed and memory."
 ],
 "checked_line": "Read by AgentGid from 11 official pages, card checked Nov 10, 2026; numbers update daily.",
 "logo": "https://agentgid.com/media/avatars/websockets.png"
}