![Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility](https://cdn.sanity.io/images/cgdhsj6q/production/97774ea8c88cc8f4bed2766c31994ebc38116948-1664x1366.png?w=400&fit=max&auto=format)
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
next-plugin-websocket
Advanced tools
Add WebSocket support to Next.js API routes
/api
pageyarn add next-plugin-websocket
Export a socket
handler function from a Next.js API route. The first argument will be the WebSocket
client instance and the second argument will be the original request object.
import { NextApiHandler } from "next";
import { NextWebSocketHandler } from "next-plugin-websocket";
export const socket: NextWebSocketHandler = (client, req) => {
console.log("Client connected");
client.on("message", (msg) => {
client.send(msg);
});
client.on("close", () => {
console.log("Client disconnected");
});
};
// You still need to expose a regular HTTP handler, even if you only intend to
// use this API route for WebSocket connections.
const handler: NextApiHandler = (req, res) => {
res.status(426).send("Upgrade Required");
};
export default handler;
import { appRouter } from "@/server/routers/_app";
import { createNextApiHandler } from "@trpc/server/adapters/next";
import { applyWSSHandler } from "@trpc/server/adapters/ws";
import { NextWebSocketHandler } from "next-plugin-websocket";
import { WebSocketServer } from "ws";
export const socket: NextWebSocketHandler = (client, req) => {
const wss = new WebSocketServer({ noServer: true });
applyWSSHandler({ wss, router: appRouter });
wss.emit("connection", client, req);
};
export default createNextApiHandler({
router: appRouter,
});
This plugin works by injecting a couple of micro-patches into the Next.js core in node_modules
. It also uses a syntax tree parser to ensure that it ends up in exactly the right place, which makes it more resilient to changes over time. However, there are a couple of things to be aware of when using this module:
node_modules
folder won't work, as that's how the patch is applied. This means no Yarn PnP support (yet)server.js
file generated in the standalone
output mode, or next start
). This means that serverless environments might be hit or miss depending on whether or not they provide an instance of http.Server
to Next.jsFAQs
Add WebSocket support to Next.js API routes
We found that next-plugin-websocket demonstrated a not healthy version release cadence and project activity because the last version was released 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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.