How do I proxy a websocket request? #3588
-
So I wrote a proxy to passthrough all my vite port requests to vite within my hono app.
This works fine however it throws when the websocket tries to connet for hot module reloading. Here are the details of the requet
The response actually has a status of 200 but under the transferred column in the devtools instead of size it says |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
fetch does not support WebSocket. |
Beta Was this translation helpful? Give feedback.
-
I’m not sure if this approach is better, but http-proxy-middleware works fine. To use it with Hono, you need to bind HTTPBindings to Hono. Here’s an example of the setup: import { createProxyMiddleware } from "http-proxy-middleware";
import { type HttpBindings} from "@hono/node-server";
const proxy = createProxyMiddleware({
... your proxy setup
})
const app = new Hono<{ Bindings: HttpBindings }>();
app.use("*", (c, next) => {
return new Promise((resolve, reject) => {
proxy(c.env.incoming, c.env.outgoing, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}); |
Beta Was this translation helpful? Give feedback.
I’m not sure if this approach is better, but http-proxy-middleware works fine. To use it with Hono, you need to bind HTTPBindings to Hono.
Here’s an example of the setup: