sarmadindhar's avatar

Listen Laravel Websocket Events on React App

Hi My application backend is on laravel (separate directory ) and front end is on react js, deployed on separate directory but same server I have some apis for chat messages, whenever a user sends message, my laravel backend broadcasts event using laravel websockets https://github.com/beyondcode/laravel-websockets

I want to listen these event on my frontend app on a specific port , please guide how can i listen these events using laravel echo or websocket.

Note: my laravel websocket server currently running on port 6001 my react app is on port 3000 while backend server is on port 8000

0 likes
11 replies
MostafaGamal's avatar

this video will help you in case you want to Self-Host your own WebSocket. I tested all the steps in the video and it works perfectly.

sarmadindhar's avatar

I have configured the server , for websockets its working properly , but i am unable to listen for events triggered by server on a separate react application

MostafaGamal's avatar

Do you use sanctum to communicate with your Laravel app?

MostafaGamal's avatar

Add this to your main.js in react app:

window.Echo = new Echo({
  broadcaster: 'pusher',
  key: 'Your key in .env file',
  wsHost: 'Your Laravel App IP',
  wsPort: 6001,
  wssport: 6001,
  transports: ['websocket'],
  enabledTransports: ['ws', 'wss'],
  forceTLS: false,
  disableStats: true
})

don't forget to install pusher-js and laravel-echo

1 like
sarmadindhar's avatar

the connection has been established with server successfully but i am unable to listen event

 window.Echo.channel('message').listen('NewMessage', (event) => {
               console.log(event);
            });

event is being triggered , i have confirmed on debugger and terminal , but i am unable to listen

MostafaGamal's avatar

Your backend app and your frontend app in the same domain ??

sarmadindhar's avatar

yes both are on same domain but in different directories , whenever the connection is successful with backend I get success message also, and channel is also subscribed (i am tracking in laravel websockets debugger panel),

but i am unable to listen it

sarmadindhar's avatar

@yacinsantos

I fixed it by changing channel name in listenere for example you have event NEW_MESSAGE broadcasting from laravel then in your client side insted of listening to NEW_MESSAGE event listen for ".NEW_MESSAGE" or add dot before event

ECHO.listen("NEW_MESSAGE") 
to 

ECHO.listen(".NEW_MESSAGE")
1 like
yacinsantos's avatar

@sarmadindhar Thank you for your reply, it is working now .. but not in all browsers, it worked for me perfectly in Edge browser, but not in chrome and brave I keep getting this error "runtime.lasterror the message port closed before a response was received" any idea why ?

MostafaGamal's avatar

Did you require pushe-js in main.js file ??

window.pusher = require('pusher-js')

Make sure you broadcast on public channel.

See if you have any message on your browser console

1 like

Please or to participate in this conversation.