Comments (3)
Heya! The socket
handler you expose in your Next.js API route is directly mapped to the handleUpgrade(req, socket, head) callback from the ws library. There are a couple of methods you could use to implement a broadcast, but the following method is how I'd go about it.
Every time you get a socket connection, store it in a Map
or a Set
:
import { NextApiHandler } from "next";
import { NextWebSocketHandler } from "next-plugin-websocket";
import { WebSocket } from "ws";
const clients = new Set<WebSocket>();
export const socket: NextWebSocketHandler = (client, req) => {
// Add the client to the set when it connects
clients.add(client);
// Remove the client from the set when it disconnects
client.on("close", () => {
console.log("Client disconnected");
clients.delete(client);
});
};
// When you call the API route, broadcast to all clients, for example
const handler: NextApiHandler = (req, res) => {
clients.forEach((client) => client.send("Hello, world!"));
res.end();
};
export default handler;
You could probably also do the same using a new WebSocketServer({ noServer: true }) instance. I shall update the docs soon with some more examples on how you might go about doing this.
It would also be possible to pass the internal WebSocketServer
instance to the socket
handler if that was absolutely necessary, but I've decided against that for now as it's responsible for handling all WebSocket connections for the whole server, regardless of whichever API route received the request.
from next-plugin-websocket.
If you wanted to implement a "send to a specific client" feature (e.g. instant messaging), instead of using a Set
, use a Map<string, WebSocket>
. The key would need to be some unique client identifier (you can either generate this or you can obtain it from the req
, depending on your business logic). Then when you need to send to a particular client, find their socket instance in the Map
and send to it.
from next-plugin-websocket.
Thanks for the detailed response!
I was able to achieve what I wanted to do through your example. I was looking for wss.clients
, but storing the clients and then sending messages via a loop works as well. Thanks so much!
from next-plugin-websocket.
Related Issues (7)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next-plugin-websocket.