{"id":40355533,"url":"https://github.com/k0d13/next-ws","last_synced_at":"2026-04-02T11:35:54.521Z","repository":{"id":172906675,"uuid":"649420029","full_name":"k0d13/next-ws","owner":"k0d13","description":"WebSockets inside Next.js app routes","archived":false,"fork":false,"pushed_at":"2026-04-01T09:04:31.000Z","size":1973,"stargazers_count":316,"open_issues_count":5,"forks_count":24,"subscribers_count":3,"default_branch":"main","last_synced_at":"2026-04-01T09:23:34.027Z","etag":null,"topics":["module","node"],"latest_commit_sha":null,"homepage":"https://npmjs.com/next-ws","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/k0d13.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"k0d13","ko_fi":"apteryx"}},"created_at":"2023-06-04T19:31:11.000Z","updated_at":"2026-04-01T09:02:26.000Z","dependencies_parsed_at":null,"dependency_job_id":"dd7c44d3-69f0-4f00-94a4-1c618a893120","html_url":"https://github.com/k0d13/next-ws","commit_stats":{"total_commits":80,"total_committers":3,"mean_commits":"26.666666666666668","dds":"0.025000000000000022","last_synced_commit":"ad059a5c18f5273c6d692bd6d1f49d416be9eaf4"},"previous_names":["apteryxxyz/next-ws","k0d13/next-ws"],"tags_count":33,"template":false,"template_full_name":null,"purl":"pkg:github/k0d13/next-ws","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k0d13%2Fnext-ws","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k0d13%2Fnext-ws/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k0d13%2Fnext-ws/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k0d13%2Fnext-ws/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/k0d13","download_url":"https://codeload.github.com/k0d13/next-ws/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k0d13%2Fnext-ws/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31305663,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-02T09:48:21.550Z","status":"ssl_error","status_checked_at":"2026-04-02T09:48:19.196Z","response_time":89,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["module","node"],"created_at":"2026-01-20T10:01:48.714Z","updated_at":"2026-04-02T11:35:54.494Z","avatar_url":"https://github.com/k0d13.png","language":"TypeScript","funding_links":["https://github.com/sponsors/k0d13","https://ko-fi.com/apteryx"],"categories":[],"sub_categories":[],"readme":"\u003cdiv align='center'\u003e\n  \u003ch1\u003e\u003cstrong\u003eNext WS\u003c/strong\u003e\u003c/h1\u003e\n  \u003ci\u003eAdd support for WebSockets in Next.js app directory\u003c/i\u003e\u003cbr\u003e\n  \u003ccode\u003enpm install next-ws ws\u003c/code\u003e\n\u003c/div\u003e\n\n\u003cdiv align='center'\u003e\n  \u003cimg alt='package version' src='https://img.shields.io/npm/v/next-ws?label=version'\u003e\n  \u003cimg alt='total downloads' src='https://img.shields.io/npm/dt/next-ws'\u003e\n  \u003cbr\u003e\n  \u003ca href='https://github.com/k0d13/next-ws'\u003e\u003cimg alt='next-ws repo stars' src='https://img.shields.io/github/stars/k0d13/next-ws?style=social'\u003e\u003c/a\u003e\n  \u003ca href='https://github.com/k0d13'\u003e\u003cimg alt='k0d13 followers' src='https://img.shields.io/github/followers/k0d13?style=social'\u003e\u003c/a\u003e\n  \u003ca href='https://discord.gg/B2rEQ9g2vf'\u003e\u003cimg src='https://discordapp.com/api/guilds/829836158007115806/widget.png?style=shield' alt='discord shield'/\u003e\u003c/a\u003e\n\u003c/div\u003e\n\n## 🤔 About\n\n`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.\n\n\u003e [!IMPORTANT]  \n\u003e 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.\n\n## 🏓 Table of Contents\n\n- [📦 Installation](#-installation)\n- [🚀 Usage](#-usage)\n- [🌀 Examples](#-examples)\n\n## 📦 Installation\n\nTo 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:\n\n1. **Install Dependencies**: Use your preferred package manager to install `next-ws` and its peer dependency `ws`:\n\n   ```bash\n   npm install next-ws ws\n   pnpm add next-ws ws\n   yarn add next-ws ws\n   ```\n\n2. **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:\n\n   ```json\n   {\n     \"scripts\": {\n       \"prepare\": \"next-ws patch\"\n     }\n   }\n   ```\n\n## 🚀 Usage\n\nUsing 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 `UPGRADE` functions.\n\n```js\nexport function UPGRADE(\n  client: import('ws').WebSocket,\n  server: import('ws').WebSocketServer,\n  request: import('next/server').NextRequest,\n  context: import('next-ws/server').RouteContext\u003c'/api/ws'\u003e,\n) {\n  // ...\n}\n```\n\n## 🌀 Examples\n\n\u003e [!TIP]  \n\u003e For more detailed examples, refer the [`examples` directory](https://github.com/k0d13/next-ws/tree/main/examples).\n\n### Echo Server\n\nThis 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 `UPGRADE` function to handle WebSocket connections:\n\n```ts\n// app/api/ws/route.ts (can be any route file in the app directory)\n\nexport function UPGRADE(\n  client: import('ws').WebSocket,\n  server: import('ws').WebSocketServer\n) {\n  console.log('A client connected');\n\n  client.on('message', (message) =\u003e {\n    console.log('Received message:', message);\n    client.send(message);\n  });\n\n  client.once('close', () =\u003e {\n    console.log('A client disconnected');\n  });\n}\n```\n\nYou can now connect to your WebSocket server, send it a message and receive the same message back.\n\n### Chat Room\n\nSee the [chat room example](https://github.com/k0d13/next-ws/tree/main/examples/chat-room).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fk0d13%2Fnext-ws","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fk0d13%2Fnext-ws","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fk0d13%2Fnext-ws/lists"}