You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a use-case where a webhook needs to be able to emit a message to all connected clients. Is it a bad idea to add the web socket server to hono's context like I'm doing via this middleware? It works but I don't know hono well enough to know if this has some gotchas that I'm ignorant of.
import"dotenv/config";import{createNodeWebSocket}from"@hono/node-ws";import{auth}from"@tasked-api/auth";import{cors}from"hono/cors";import{createFactory}from"hono/factory";import{logger}from"hono/logger";importtype{WebSocketServer}from"ws";typeEnv={Variables: {wss: WebSocketServer;};};consthonoFactory=createFactory<Env>();constwssMiddleware=(wss: WebSocketServer)=>honoFactory.createMiddleware(async(c,next)=>{if(!c.var.wss){c.set("wss",wss);}returnnext();});constapp=honoFactory.createApp();exportconst{ injectWebSocket, upgradeWebSocket, wss }=createNodeWebSocket({
app,});app.use(wssMiddleware(wss));app.use(logger());app.get("/ws",upgradeWebSocket((c)=>({onClose(evt,ws){console.log("WebSocket connection closed");},onError(evt,ws){console.error("WebSocket error observed:",evt);},onMessage(evt,ws){console.log("WebSocket message received:",evt);},onOpen(evt,ws){console.log("WebSocket connection opened");},})),);app.get("/",(c)=>{returnc.html(`<html> <head> <title>WebSocket Test</title> <script> let socket; function init() { socket = new WebSocket("ws://" + location.host + "/ws"); socket.onopen = function(event) { console.log("WebSocket is open now."); }; socket.onmessage = function(event) { console.log("WebSocket message received:", event); }; socket.onclose = function(event) { console.log("WebSocket is closed now."); }; } window.onload = init; </script> </head> <body> <h1>WebSocket Test Page</h1> <p>Open the console to see WebSocket events.</p> </body> </html>`);});app.get("/webhook",(c)=>{constwss=c.get("wss");// could of course just forgo this line and just use wss from createNodeWebSocketwss.clients.forEach((client)=>{client.send("Hello from /webhook!");});returnc.text("Webhook sent messages to all WebSocket clients.");});constserver=serve({fetch: app.fetch,port: 3000,},(info)=>{console.log(`Server is running on http://localhost:${info.port}`);},);injectWebSocket(server);
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a use-case where a webhook needs to be able to emit a message to all connected clients. Is it a bad idea to add the web socket server to hono's context like I'm doing via this middleware? It works but I don't know hono well enough to know if this has some gotchas that I'm ignorant of.
Beta Was this translation helpful? Give feedback.
All reactions