Ever wanted your browser to talk directly to the Vite dev server? Whether you're building custom devtools, triggering rebuilds, or just logging user behavior during development — it's totally possible using Vite’s built-in WebSocket connection.
Vite's dev server runs a WebSocket behind the scenes to power features like hot module replacement (HMR). With a small trick, you can use this connection to send your own custom messages from the browser to the server — no polling or HTTP requests needed.
Inside your app code, you can use import.meta.hot.send()
to dispatch an event to the Vite server:
if (import.meta.hot) {
import.meta.hot.send('my-custom-event', {
message: 'Hello from the browser!'
});
}
This only runs in development (since import.meta.hot
is only defined in dev mode), making it perfect for debugging or building custom dev features.
In your vite.config.ts
, use the configureServer
hook to handle incoming events:
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
{
name: 'custom-dev-events',
configureServer(server) {
server.ws.on('my-custom-event', (data) => {
console.log('Received custom event:', data.message);
});
}
}
]
});
Now whenever that event is sent from the browser, your dev server can respond — log it, run a script, send something back, or whatever else you need.
You can also send a message back to the browser:
server.ws.send({
type: 'custom-response',
payload: { success: true }
});
And receive it on the client:
if (import.meta.hot) {
import.meta.hot.on('custom-response', (data) => {
console.log('Got server response:', data.success);
});
}
import.meta.hot.send()
to send events to the dev servervite.config.ts
via server.ws.on(...)
This is a super useful trick for building smoother tooling experiences, interactive playgrounds, or advanced logging without any production impact.
To see this in action view this example on stackblitz.
If you’d like more info on using or customizing Vite (including building your own Vite plugins), checkout our course Rapid Development with Vite.
Our goal is to be the number one source of Vue.js knowledge for all skill levels. We offer the knowledge of our industry leaders through awesome video courses for a ridiculously low price.
More than 200.000 users have already joined us. You are welcome too!
© All rights reserved. Made with ❤️ by BitterBrains, Inc.