Node Web Sockets in Hono
Hello all, I'm fairly new to Hono and I've recently picked it up for some of my projects and enjoyed it very much.
Im currently facing problems with one of my projects that uses WebSockets to communicate with Hardware boxes. And I will need another WebSocket as well to communicate with my frontend and mobile application.
I've searched for many solutions online but it looks like Hono with Node WebSockets is rarely talked about.
After a lot of trial and error I finally reached this code to let my socket run with Hono
[1/3]
26 Replies
[2/3]
However this doesn't look like good code at all and I was expecting it to give me errors in the future. Which it did.
I used
Better-Auth
with Hono and here is were my problems started showing. A lot of better auth functionalities got limited and some break because of this code. And I'm pretty sure later on other things will break because of this
[3/3]
Is there a better way to allow Web socket to run with Hono without creating a server?I like the fake parts,
[1/?]
Lol i had no idea how many posts i will do xD
I am pretty sure the docs show exactly that?
all parts are
part = 1
that's what I meantI looked for it, they only show for Deno and Bun and Cloudflare workers
https://hono.dev/docs/helpers/websocket
WebSocket Helper - Hono
Web framework built on Web Standards for Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Node.js, and others. Fast, but not only fast.

Im blind
we all are
props to you for pushing through with that hacky way though!
not giving up is an important skill
but carefully reading the docs is another important skill
ā
import { Hono } from 'hono';
import { ws } from 'hono/ws';
const app = new Hono();
// Define a WebSocket route
app.get(
'/ws',
ws((ws) => {
console.log('WebSocket connection established.');
// Handle incoming messages
ws.onmessage = (event) => {
console.log('Received:', event.data);
ws.send(
Echo: ${event.data}
);
};
// Handle connection close
ws.onclose = () => {
console.log('WebSocket connection closed.');
};
})
);
// Start the app
app.fire();
āyeah I am sure they can read the docs, no need for snippets
I was wondering about that lol, here is an example by chatgpt
Thanks! Now I have to go rewrite most of my code š
I'll mark this as solved for now
well, I hope it is a rewarding experience
The funny part is I went to GPT4o to ask for a hono node ws and it came up with something similar to OP
I had to also remind it of the middleware to get the above
is GPT4 actually using the LLM.txt provided by the docs?
So dont feel bad even AI isnt aware lol
if so, sweet
Rewarding is one word for it
Not sure
I dont think so, i rarely use GPT4
But AI's in general are not up to date with the latest Hono tech thats for sure
maybe you can directly tell it to search through the LLM.txt?
e.g.
it actually worked
Interesting thet GPT could do it and not Claude
I did have "Search" enabled as a tool
does claude have smth similar?
as an alternative, you can provide https://hono.dev/llms-full.txt as a resource instead
iirc they call it "embedding"
or smth
RIP my wallet. But yeah i guess Claude cant do this now, or at least on the free version. GPT did it without the "serach" enabled though it wasnt as accurate as the answer you got
DuckDuckGo AI Chat at DuckDuckGo
DuckDuckGo. Privacy, Simplified.