Skip to content

feat: add useSocketId() hook to React, Vue and Svelte adapters#499

Open
hectorgrecco wants to merge 3 commits into
laravel:2.xfrom
hectorgrecco:add-use-socket-id
Open

feat: add useSocketId() hook to React, Vue and Svelte adapters#499
hectorgrecco wants to merge 3 commits into
laravel:2.xfrom
hectorgrecco:add-use-socket-id

Conversation

@hectorgrecco
Copy link
Copy Markdown

@hectorgrecco hectorgrecco commented May 14, 2026

Summary

Adds a useSocketId() reactive hook/composable/rune to all three framework adapters, closing the gap described in #474.

The existing echo() function is already exported from all adapters and can be used directly for interceptor setup:

import { echo } from '@laravel/echo-react';

axios.interceptors.request.use((config) => {
    const socketId = echo().socketId();
    if (socketId) config.headers['X-Socket-Id'] = socketId;
    return config;
});

However, there was no reactive primitive for component usage that automatically updates when the connection reconnects and receives a new socket ID. This PR fills that gap.

New API

React

import { useSocketId } from '@laravel/echo-react';

const socketId = useSocketId(); // string | undefined, re-renders on reconnect

Vue

import { useSocketId } from '@laravel/echo-vue';

const socketId = useSocketId(); // ComputedRef<string | undefined>

Svelte

import { useSocketId } from '@laravel/echo-svelte';

const socketId = useSocketId(); // () => string | undefined (getter, Svelte 5 runes pattern)

Implementation

useSocketId() builds on the existing useConnectionStatus() in each adapter — it subscribes to onConnectionChange for free, so when the connection cycles (and a new socket ID is assigned), the value updates automatically. No new subscriptions or state management.

Test plan

  • packages/react: 61 tests pass, 1 pre-existing skip
  • packages/vue: 45 tests pass, 2 pre-existing skips
  • packages/svelte: 32 tests pass

Closes #474

Implements useSocketId hook that calls useConnectionStatus for reactivity and returns echo().socketId(). Also adds socketId and connector mocks to the laravel-echo vi.mock to support the new test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No official way to access socketId when using Reverb + toOthers() with @laravel/echo-react

1 participant