Home / Blog / How to Send Real Time Custom Events from the Browser a Vite Dev Server
How to Send Real Time Custom Events from the Browser a Vite Dev Server

How to Send Real Time Custom Events from the Browser a Vite Dev Server

Daniel Kelly
Daniel Kelly
Updated: June 11th 2025

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.

Why Send Events to the Dev Server?

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.

Step 1: Send a Custom Event from the Browser

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.

Step 2: Listen for the Event on the Vite Server

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.

Bonus: Two-Way Communication

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);
  });
}

Recap

  • Use import.meta.hot.send() to send events to the dev server
  • Handle them in vite.config.ts via server.ws.on(...)
  • Works great for dev-only workflows, no extra network layer needed

This is a super useful trick for building smoother tooling experiences, interactive playgrounds, or advanced logging without any production impact.

If you’d like more info on using or customizing Vite (including building your own Vite plugins), checkout our course Rapid Development with Vite.

Start learning Vue.js for free

Daniel Kelly
Daniel Kelly
Daniel is the lead instructor at Vue School and enjoys helping other developers reach their full potential. He has 10+ years of developer experience using technologies including Vue.js, Nuxt.js, and Laravel.

Comments

Latest Vue School Articles

GitHub Login in Under 10 Mins with Nuxt Auth Utils

GitHub Login in Under 10 Mins with Nuxt Auth Utils

Learn how to quickly integrate GitHub authentication into your Nuxt 3 application using the Nuxt Auth Utils module. Covers setup, server routes, and useUserSession composable.
Daniel Kelly
Daniel Kelly
A Custom Opinionated Event Handler for Nuxt API Endpoints with Guards And Validation Support

A Custom Opinionated Event Handler for Nuxt API Endpoints with Guards And Validation Support

Nuxt API endpoints are extremely useful! Boost your productivity with some handy conventions. Learn to create custom event handlers, advanced validation techniques with zod, and endpoints protection with guards.
Daniel Kelly
Daniel Kelly
VueSchool logo

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!

Follow us on Social

© All rights reserved. Made with ❤️ by BitterBrains, Inc.