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 13th 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

Why AI responses Are Never the Same and How to Fix It for Developers

Why AI responses Are Never the Same and How to Fix It for Developers

Why do AI responses like ChatGPT’s vary? Learn why and how to get accurate answers for Vue.js and web development in 2025 with practical tips.
Eleftheria Batsou
Eleftheria Batsou
From CSS to Shaders

From CSS to Shaders

Discover why traditional CSS and JavaScript fall short when building smooth, interactive ripple effects in modern web interfaces. Learn how GPU-powered WebGL shaders offer a performant, scalable alternative for creating fluid, responsive animations. This article breaks down the limitations of DOM-based animations and introduces you to the power and elegance of shader-based effects.
Simon Le Marchant
Simon Le Marchant
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.