Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dyiroooo's avatar

Results of broadcast is showing on Pusher's Debug Console, yet on my Vuejs Component can't seem to listen to the broadcast

My pusher and broadcast setup seems working because I can see the API Message on Pusher's debug console yet I cannot listen to the broadcast. This is how I setup it on my VueJS 2 Component. Thank you for any answers or response.

	import Echo from "laravel-echo";
	window.Pusher = require('pusher-js');
				
	const echo = new Echo({
						broadcaster: 'pusher',
						key: 'my key',
						cluster: 'my cluster',
						encrypted: true,
						authEndpoint: 'pusher/auth',
						auth: {
    						headers: {
        						Authorization: 'Bearer ' + localStorage.getItem('token')
    						},
						}
					});

						// this is under my methods component and I call it on mounted
    listenForPrivateChannelEvents() {

        try {
            echo.channel(`change-price.${this.productId}`)
                .listen('ChangePrice', (event) => {
                    this.handlePriceChange(event.productId, event.newPrice);
                });

        } catch (error) {
            console.error("Error subscribing to private channel:", error);
        }
    },
    handlePriceChange(productId, newPrice) {
        console.log(`Price for product ${productId} changed to ${newPrice}`);
    },
0 likes
43 replies
gych's avatar

Can you try like this?

window.Echo.channel("change-price." + this.productId).listen(".ChangePrice", (event) =>
dyiroooo's avatar

@gych thanks, I did try it but it is still the same. Also when I downgrade the version of laravel-echo and pusher-js I encountered a new error that is showing on my console which is;

pusher.js:613 Pusher : Couldn't get auth info from your webapp : 404

and

POST pusher/auth 404 (Not Found) 
gych's avatar

@dyiroooo I just see that you import it directly in your file with echo so you don't have to use window.Echo, my bad. I mostly import pusher and echo in bootstrap js file

Just try it like this then:

echo.channel("change-price." + this.productId).listen(".ChangePrice", (event) =>

Make sure that ChangePrice has a class . at the beginning

1 like
dyiroooo's avatar

@gych thank you so much the errors are gone but I still can't console.log .listen

gych's avatar

@dyiroooo Can you activate logging for pusher? Then you can get logs about pusher in console, its esier for debugging whats wrong

like this

Pusher = require('pusher-js');

window.Pusher = Pusher;

Pusher.logToConsole = true;
dyiroooo's avatar

I added the Pusher.logToConsole and indeed it was helpful, whenever I am using the method on my vuejs it consoles these:

Pusher : Event sent : {"event":"pusher:ping","data":{}}
Pusher : Event recd : {"event":"pusher:pong","data":{}}

Edit: I am wondering, why is the data property is empty? and is it the API message from pusher's debug console?

gych's avatar

@dyiroooo It looks like pusher is not successfully connecting

Because when a connection has been made it should give you for example these logs in browser console:

Pusher :  : ["Event sent",{"event":"pusher:subscribe","data":{"auth":"...","channel":"change-price.1"}}]
pusher-js.js?v=a1f66f88:957 Pusher :  : ["Event recd",{"event":"pusher_internal:subscription_succeeded","channel":"change-price.1","data":{}}]
dyiroooo's avatar

@gych

Pusher : Event sent : {"event":"pusher:subscribe","data":{"channel":"book-a-job.2"}} pusher.js:161 Pusher : Event recd : {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"book-a-job.2"}

this is now occurring on my console.log, does it mean it is now connecting successfully?

dyiroooo's avatar

if it is connecting successfully, the only problem left is the .listen class is not working

gych's avatar

@dyiroooo yes you should .bind instead, like you showed in one of your previous replies

dyiroooo's avatar

@gych .bind is not known by the Echo, instead of using .bind, the pusher's documentation told me to use .listen since I am using Laravel Echo.

There are no errors existing right now but I can't seem to console.log the event's data from the .listen

gych's avatar

@dyiroooo But you are not using Laravel Echo in your vue project right?

From what I know now, you use Laravel Echo in Laravel project. And just pusher in the vue project, echo can only be used in the vue project after installing a specific package for that but I think you haven't done that yet? And your setup will be completly different than what you changed it to now.

dyiroooo's avatar

@gych I am using Laravel Echo and I did install a specific package which are; pusher-js and laravel-echo. This is so hard to configure.

I am using a PrivateChannel and when I use this code: const channel = echo.channel(change-price. + booking_id); I can subscribe and connect with no problem but the .listen seems not to work

but when I am using: this const channel = echo.private(change-price. + booking_id); or this const channel = echo.channel(private-change-price. + booking_id); the error POST http://localhost:8080/pusher/auth 404 (Not Found) shows Pusher : Couldn't get auth info from your webapp : 404

gych's avatar

Can you also show the code of your ChangePrice event

dyiroooo's avatar

@gych class ChangePrice implements ShouldBroadcast { public $bookingId; public $newPrice;

public function __construct($bookingId, $newPrice)
{
    $this->bookingId = $bookingId;
    $this->newPrice = $newPrice;

    Log::info("ChangePrice event fired for booking number $bookingId with new price $newPrice");
}

public function broadcastOn()
{
    $channel = new PrivateChannel("book-a-job.{$this->bookingId}");
    Log::info('Broadcasting on channel:', ['channel' => $channel]);

    return $channel;
}

}

gych's avatar

@dyiroooo I see that you are using a privatechannel for private channels you should use this instead: echo.private("change-price." + this.productId).listen(".ChangePrice", (event) =>

dyiroooo's avatar

@gych I already did now, errors are only showing when the page is first reloaded but when I trigger the method, it does not show anything and the data result still reflects on the pusher's debug console

gych's avatar

@dyiroooo Its because your data is send from server back-end and that seems to work fine because you can see it in the pusher debug console. But the issue you have right now is that the client side (front-end) is not successfully making connection

dyiroooo's avatar

what should I do? these are some of the errors that also occur

Pusher : State changed : initialized -> connecting

Pusher : Connecting : {"transport":"ws","url":"wss://ws-ap1.pusher.com:443/app/0dd0b3e095a8a8830701?		protocol=7&client=js&version=3.2.4&flash=false"}

Pusher : State changed : connecting -> connected with new socket ID 28777.3677496
pusher.js:585 
    
    // this error is back again
    POST mylocalhost/pusher/auth 404 (Not Found)
	Pusher : Couldn't get auth info from your webapp : 404

	Pusher : Couldn't get auth info from your webapp : 404
	Pusher : No callbacks on private-book-a-job.undefined for pusher:subscription_error

Edit: I changed some variables

gych's avatar

@dyiroooo Try to connect with this instead

const echo = new Echo({
    broadcaster: 'pusher',
    key: import.meta.env.VITE_PUSHER_APP_KEY,
    cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER,
    forceTLS: true
});

Also make sure you have this defined in your env file:

# PUSHER
PUSHER_APP_ID=YOUR APP ID
PUSHER_APP_KEY=YOUR APP KEY
PUSHER_APP_SECRET=YOUR APP SECRET
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=YOUR APP CLUSTER (for me its eu but could be different for you)

VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
dyiroooo's avatar

@gych my .env pusher is setup-ed like how you use it but my VueJS project folder is separated from my laravel folder so it's like I'm only using axios to make them communicate.

backend-laravel (root) // serve using php artisan serve

frontend-vuejs (root) // serve using npm run serve

gych's avatar

@dyiroooo Ok thanks for clarifying that and did you also add the variables in your vue project? And do you use vite or webpack?

dyiroooo's avatar

@gych yes I added the variables but I don't use either vite or webpack? How should I do that?

gych's avatar

@dyiroooo What do you currently use to build your vue application for production?

dyiroooo's avatar

@gych Frontend:

  • VueJS 2
  • VueX
  • Vuetify

Backend:

  • Laravel
  • Laravel Passport
gych's avatar

@dyiroooo Ok I guess you use vue CLI then, which version of CLI do you use?

dyiroooo's avatar

@gych I don't know the version (I tried in my cmd vue -v, but it says that its not recognized) but here are some my dependencies

"devDependencies": { "@vue/cli-plugin-router": "~5.0.0", "@vue/cli-plugin-vuex": "~5.0.0", "@vue/cli-service": "~5.0.0", "sass": "~1.32.0", "sass-loader": "^10.0.0", "vue-cli-plugin-vuetify": "^2.5.8", "vue-template-compiler": "^2.6.14", "vuetify-loader": "^1.7.0" }

gych's avatar
gych
Best Answer
Level 29

Ok I see that you use vue CLI 5 Also Echo is a feature from Laravel but because your vue2 and laravel are separated this doesn't work. You should use the js examples from pusher documentation.

What is the goal you currently want to achieve by using pusher? Think about if its not possible to just use axios calls for this.

Also because you're using a private channel make sure to add private- infront of the event name like this private-change-price

In this event the channel name is also different than the channel you use in your js script Make sure to also broadcast an event for change-price

public function broadcastOn()
{
    $channel = new PrivateChannel("book-a-job.{$this->bookingId}");
    Log::info('Broadcasting on channel:', ['channel' => $channel]);

    return $channel;
}

Check these links from Pusher documentation: Pusher JS Quickstart: https://pusher.com/docs/channels/getting_started/javascript/

Use case on how to authenticate pusher for your project: https://pusher.com/docs/channels/getting_started/javascript-realtime-user-list/

1 like
dyiroooo's avatar

@gych I will try this. What I wanted to achieve is a user can bargain a price for another user and they will be notified if the other user has changed the price, since I don't want to use laravel websockets I'm sticking to laravel-echo and pusher-js.

Thanks for your help, it means a lot!

gych's avatar

@dyiroooo You're welcome

Also check my last reply on this post, its about a package that lets you use laravel echo in a standalone vue app like in your case, might be easier than using the docs from pusher.

If you have more questions, let me know

1 like
dyiroooo's avatar

@gych I did follow the pusher's documentation codes and I see that I am having trouble connecting to pusher I guess because the error I keep getting is this:

Pusher : Error : {"type":"WebSocketError","error":{"type":"PusherError","data":{"code":4005,"message":"Path not found"}}}
gych's avatar

@dyiroooo This mostly means that your project can't make connection with pusher Are you sure that you have added the correct details here?

var pusher = new Pusher({
  appId: "APP_ID",
  key: "APP_KEY",
  secret: "APP_SECRET",
  cluster: "APP_CLUSTER",
});
gych's avatar

@dyiroooo Did you add them from the env file or did you just add them directly in the code?

dyiroooo's avatar

@gych I just added them direct in my component's code, should I make an .env file for my VueJS project?

gych's avatar

@dyiroooo For testing purposes you can add it directly in your component's code but when pushing to code to git or deploying it to your host you should move those details to the env file for security reasons

Can you share the part of your code that has been updated ?

dyiroooo's avatar

@gych I've added an .env file, and my echo-config.js now looks like this

import Echo from "laravel-echo"; import Pusher from "pusher-js";

window.Pusher = Pusher; Pusher.logToConsole = true;

var pusher = new Pusher({ appId: "my-appId", key: process.env.VUE_APP_PUSHER_APP_KEY, cluster: process.env.VUE_APP_PUSHER_APP_CLUSTER, secret: process.env.VUE_APP_PUSHER_APP_SECRET, // encrypted: true, channelAuthorization: { endpoint: "my-end-point", }, });

export default pusher;

dyiroooo's avatar

also, I followed the class used in the pusher's documentation such as .subscribe for connecting and .bind for listening an event

    const channel = echo.subscribe(`book-a-job.`  + booking_id);
    channel.bind('.ChangePrice', (event) => {
        this.handlePriceChange(event.productId, event.newPrice);
    });
gych's avatar

@dyiroooo

In the docs I see that this is the config structure you should have in your component

    const pusher = new Pusher(
       	process.env.VUE_APP_PUSHER_APP_KEY,
        {
          cluster: process.env.VUE_APP_PUSHER_APP_CLUSTER,
          forceTLS: true,
          channelAuthorization: {
            endpoint: "your-endpoint",
          }
        }
      );

and this in the auth file

const pusher = new Pusher({
  appId: "APP_ID", // Replace with 'app_id' from dashboard
  key: "APP_KEY", // Replace with 'key' from dashboard
  secret: "APP_SECRET", // Replace with 'secret' from dashboard
  cluster: "APP_CLUSTER", // Replace with 'cluster' from dashboard
  useTLS: true,
});

Also check if you actually get the correct values from the env file. You can test this by logging process.env.VUE_APP_PUSHER_APP_KEY to console.

dyiroooo's avatar

@gych it is all now working man, thanks a lot for your guidance and patience

gych's avatar

@dyiroooo Glad I could help! Don't forget to close your thread by selecting the best answer.

If you need further assistance, don't hesitate to reach out.

1 like

Please or to participate in this conversation.