beyondcode / laravel-websockets

Websockets for Laravel. Done right.
https://beyondco.de/docs/laravel-websockets
MIT License
5.07k stars 628 forks source link

Uncaught TypeError: Cannot read property 'apply' of undefined #573

Closed demorgia closed 9 months ago

demorgia commented 4 years ago

Hi, I am trying to integrate web sockets server for PUSHER API replacement in my project. I am using Laravel 6.2 with mysql and also I have integrated CoreUI Admin panel in my project. I am following the official documentation at https://beyondco.de/docs/laravel-websockets/getting-started/introduction for the same.

I have everything working fine on public channel, but while sending the event notification to client I am facing an error at client side saying

app.js:40301 Uncaught TypeError: Cannot read property 'apply' of undefined at Channel.Dispatcher.emit (app.js:40301) at Channel.handleEvent (app.js:41964) at app.js:38832 at ConnectionManager.Dispatcher.emit (app.js:40301) at message (app.js:46906) at Connection.Dispatcher.emit (app.js:40301) at message (app.js:41694) at TransportConnection.Dispatcher.emit (app.js:40301) at TransportConnection.onMessage (app.js:40198) at WebSocket.socket.onmessage (app.js:40215) Dispatcher.emit @ app.js:40301 Channel.handleEvent @ app.js:41964 (anonymous) @ app.js:38832 Dispatcher.emit @ app.js:40301 message @ app.js:46906 Dispatcher.emit @ app.js:40301 message @ app.js:41694 Dispatcher.emit @ app.js:40301 TransportConnection.onMessage @ app.js:40198 socket.onmessage @ app.js:40215

It seems there is some issue in the callback method, I am struggling with this for few days now and is not able to identify the cause or fix for this. My pusher js version is 4.4.0 and laravel-echo version is 1.8.1

The event is getting notified successfully to the web sockets server and also the client is able to make successful connection with web sockets server, therefore now I have replaced the pusher service with my local server instance. Also I am using this code in blade file for event callback for the client to listen to

    Echo.channel('home')
        .listen('NewMessage'), e => {
            console.log(e.message);
        alert(JSON.stringify(e.message));
    }

Kindly suggest what configuration error I might be doing which is causing this issue.

Thanks, Saurabh

raffobaffo commented 3 years ago

If you pasted the exact code, to me it seems there is a syntax error, this would work:

.listen('NewMessage', e => {
        console.log(e.message);
        alert(JSON.stringify(e.message)
        });