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

Muetze's avatar
Level 37

Receive Laravel Echo/Reverb/Broadcasting Events outside Laravel in Node.js

I use Laravel Echo and Reverb with unauthenticated public channels. In Laravel with Vue it works perfectly:

  Echo.channel('imports').listen('Broadcasting.AddImport', (event: AddImportInterface) => {
    console.log('event-vue', event.process)
  })

But if I want to access it via a NodeJs script, I can't use Laravel Echo. I get this error:

dist/echo.js:904
        throw new Error('Pusher client not found. Should be globally available or passed via options.client'

And with the option for client:

import Pusher from 'pusher-js'

new Echo({
  // ...
  client: Pusher
})

Following error:

echo.js:217
      this.subscription = this.pusher.subscribe(this.name);
```j

Does anyone know how to receive the events outside of Laravel in a NodeJs script?
0 likes
1 reply
LaryAI's avatar
Level 58

To receive Laravel Echo events in a Node.js script, you can use the pusher-js library directly, as Laravel Echo is primarily designed for use in the browser. Here's how you can set it up:

  1. Install the pusher-js library: You can do this using npm.

    npm install pusher-js
    
  2. Set up a Node.js script to listen for events: Use the pusher-js library to connect to your Pusher channels and listen for events.

    Here's an example script:

    const Pusher = require('pusher-js');
    
    // Initialize Pusher with your app key and cluster
    const pusher = new Pusher('your-pusher-app-key', {
      cluster: 'your-pusher-cluster',
      encrypted: true
    });
    
    // Subscribe to the channel you want to listen to
    const channel = pusher.subscribe('imports');
    
    // Bind to the event you want to listen for
    channel.bind('Broadcasting.AddImport', function(event) {
      console.log('event-node', event.process);
    });
    
  3. Ensure your Pusher credentials are correct: Replace 'your-pusher-app-key' and 'your-pusher-cluster' with the actual credentials from your Pusher account. These should match the credentials used in your Laravel application.

  4. Run your Node.js script: Execute the script using Node.js to start listening for events.

    node your-script.js
    

This setup should allow your Node.js script to receive and handle events broadcasted from your Laravel application using Pusher. Make sure your Pusher settings in Laravel (config/broadcasting.php) are correctly configured to match the Pusher credentials you are using in your Node.js script.

Please or to participate in this conversation.