
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
npm install next-ws ws
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.
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:
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
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"
}
}
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 UPGRADE functions.
export function UPGRADE(
client: import('ws').WebSocket,
server: import('ws').WebSocketServer,
request: import('next/server').NextRequest,
context: import('next-ws/server').RouteContext<'/api/ws'>,
) {
// ...
}
[!TIP]
For more detailed examples, refer theexamplesdirectory.
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 UPGRADE function to handle WebSocket connections:
// app/api/ws/route.ts (can be any route file in the app directory)
export function UPGRADE(
client: import('ws').WebSocket,
server: import('ws').WebSocketServer
) {
console.log('A client connected');
client.on('message', (message) => {
console.log('Received message:', message);
client.send(message);
});
client.once('close', () => {
console.log('A client disconnected');
});
}
You can now connect to your WebSocket server, send it a message and receive the same message back.
See the chat room example.
FAQs
Add support for WebSockets in the Next.js app directory
The npm package next-ws receives a total of 6,060 weekly downloads. As such, next-ws popularity was classified as popular.
We found that next-ws demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.