Nuxt API

useActiveConnections()

Last updated by
Harlan Wilton
in feat: view active connections (#6).

A composable for accessing real-time statistics about active SSE/WebSocket connections and their version distribution.

This composable requires connectionTracking: true in your module config and only works with sse or ws update strategies. It does not support Pusher/Ably adapters.

Setup

Enable connection tracking in your Nuxt config:

nuxt.config.ts
export default defineNuxtConfig({
  skewProtection: {
    connectionTracking: true
  }
})

Usage

const { total, versions } = useActiveConnections()

Returns

total

  • Type: ComputedRef<number>

The total number of active connections across all versions.

const { total } = useActiveConnections()

console.log(total.value) // 42

versions

  • Type: ComputedRef<Record<string, number>>

A map of build IDs to connection counts, showing how many users are on each version.

const { versions } = useActiveConnections()

console.log(versions.value)
// { "abc123": 35, "def456": 7 }

Type Definitions

interface ConnectionStats {
  total: number
  versions: Record<string, number>
}

Examples

Admin Dashboard Widget

<script setup>
const { total, versions } = useActiveConnections()

const versionList = computed(() => {
  return Object.entries(versions.value)
    .sort(([, a], [, b]) => b - a)
    .map(([id, count]) => ({
      id: id.slice(0, 8),
      count,
      percentage: Math.round((count / total.value) * 100)
    }))
})
</script>

<template>
  <div class="connections-widget">
    <h3>Active Users: {{ total }}</h3>
    <ul>
      <li v-for="v in versionList" :key="v.id">
        {{ v.id }}: {{ v.count }} ({{ v.percentage }}%)
      </li>
    </ul>
  </div>
</template>

Deployment Safety Check

const { total, versions } = useActiveConnections()

const usersOnOldVersions = computed(() => {
  const currentBuildId = useRuntimeConfig().app.buildId
  return Object.entries(versions.value)
    .filter(([id]) => id !== currentBuildId)
    .reduce((sum, [, count]) => sum + count, 0)
})

watch(usersOnOldVersions, (count) => {
  if (count === 0) {
    console.log('All users migrated to latest version!')
  }
})

Real-time Stats Display

<script setup>
const { total, versions } = useActiveConnections()
const buildId = useRuntimeConfig().app.buildId

const stats = computed(() => {
  const current = versions.value[buildId] || 0
  const outdated = total.value - current
  return {
    current,
    outdated,
    percentCurrent: total.value ? Math.round((current / total.value) * 100) : 0
  }
})
</script>

<template>
  <div class="stats">
    <span>{{ stats.current }} current</span>
    <span>{{ stats.outdated }} outdated</span>
    <span>{{ stats.percentCurrent }}% up to date</span>
  </div>
</template>

Limitations

  • Only works with updateStrategy: 'sse' or updateStrategy: 'ws'
  • Does not support Pusher or Ably adapters (connections are managed by third-party)
  • Stats are per-server instance (not aggregated across horizontal scaling)
  • Requires server with persistent connections (not compatible with serverless/edge)
Did this page help you?