Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easily implement broadcasting in a React/Vue Typescript app (Starter Kits) #55170

Draft
wants to merge 6 commits into
base: 12.x
Choose a base branch
from

Conversation

tnylea
Copy link
Contributor

@tnylea tnylea commented Mar 25, 2025

This PR is going to make the process of implementing broadcasting in the React/Vue starter kits super simple.

This PR will detect if a users application utilizes react or vue. It will also check for the existense of the typescript dependency. Then, when the developer runs the php artisan install:broadcasting command it will install the necessary Echo hook or composable.

React

Running php artisan install:broadcasting, the use-echo-ts.stub will be installed in the resources/js/hooks folder. It can then be leveraged inside of any component, like so:

import { useEcho } from '@/hooks/use-echo';

useEcho('test-channel', 'test.event', (payload) => console.log(payload), [], 'public');

This will subscribe to test-channel and log the payload when test.event is fired.

Vue

Running php artisan install:broadcasting, the useEcho-ts.stub will be installed in the resources/js/composables folder. It can then be leveraged inside of any component, like so:

import { useEcho } from '@/composables/useEcho';

useEcho('test-channel', 'test.event', (payload) => { console.log(payload) }, [], 'public');

This is the exact same as React except the useEcho is stored in the composables folder as opposed to the hooks folder.

Configuration

Before using this hook/composable, echo needs to be configured, like so:

app.tsx

import { configureEcho } from './hooks/use-echo';

configureEcho({
    broadcaster: 'reverb',
    key: import.meta.env.VITE_REVERB_APP_KEY,
    wsHost: import.meta.env.VITE_REVERB_HOST,
    wsPort: import.meta.env.VITE_REVERB_PORT ?? 80,
    wssPort: import.meta.env.VITE_REVERB_PORT ?? 443,
    forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'https') === 'https',
    enabledTransports: ['ws', 'wss'],
});

This will allow developers to modify echo to use reverb, pusher, ably or any other websocket service. This is going to be added by default (Still adding this functionality to the install:broadcasting command. After I finish implementing the publishing of the configure, this update will be good to go!

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@tnylea tnylea marked this pull request as ready for review March 26, 2025 13:33
@taylorotwell taylorotwell marked this pull request as draft March 27, 2025 17:15
@tnylea tnylea marked this pull request as ready for review March 28, 2025 20:10
@taylorotwell taylorotwell marked this pull request as draft March 29, 2025 16:56
@taylorotwell
Copy link
Member

Drafting this while I review.

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.

2 participants