Add real-time visitor tracking to your Nuxt app with one line of code. WebSocket made easy
Add live visitor counting to your Nuxt website in seconds. WebSocket-based, type-safe, and completely automatic.
undefinedNew (Experimental): Anonymous tracking of visitors’ locations!
When enabled, the module tracks visitor locations. The
useVisitorscomposable will additionally provide:
- A
locationsarray: lists locations of all visitors.- A
myLocationobject: contains the specific geolocation data for the current visitor.undefinedNote: This feature is experimental and may be subject to future changes.
Install the module to your Nuxt application with one command:
npx nuxi module add nuxt-visitors
As Nitro WebSocket support is experimental, you need to enable it in your nuxt.config.ts:
export default defineNuxtConfig({
modules: ['nuxt-visitors'],
nitro: {
experimental: {
websocket: true
}
}
})
The module accepts an optional configuration to enable location tracking. In your nuxt.config.ts, you can enable it as follows:
export default defineNuxtConfig({
modules: ['nuxt-visitors'],
visitors: {
// Set to true to enable tracking of visitor locations
locations: true
},
nitro: {
experimental: {
websocket: true
}
}
})
This will enable the locations and myLocation properties in the composable, as well as the locations property in the composable’s return object.
<script setup lang="ts">
const { visitors } = useVisitors()
</script>
<template>
<div>Active users: {{ visitors }}</div>
</template>
That’s it! The module handles everything else automatically:
The composable provides additional features:
const {
visitors, // Ref<number> - Current visitor count
locations, // Ref<Location[]> - Array of geolocation objects for all visitors (if `locations: true`)
myLocation, // Ref<Location | null> - Geolocation of the current visitor (if `locations: true`)
isConnected, // Ref<boolean> - Connection status
isLoading, // Ref<boolean> - Loading state
error, // Ref<string | null> - Error message if any
reconnect // () => void - Manual reconnection
} = useVisitors()
To start contributing, you can follow these steps:
feat/123 or fix/456.bun run test.Published under the APACHE license.
Made by @HugoRCD and community 💛
🤖 auto updated with automd (last updated: Tue Apr 15 2025)
We use cookies
We use cookies to analyze traffic and improve your experience. You can accept or reject analytics cookies.