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:
-
Install the
pusher-jslibrary: You can do this using npm.npm install pusher-js -
Set up a Node.js script to listen for events: Use the
pusher-jslibrary 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); }); -
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. -
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.