0

I'm working on developing a quick chat application to develop my skills with pusher and decided to start getting into private channels. The public channel application I had on the same client-side code and slightly tweaked App\Events\chatmessagesent event (changed return new Channel(...) to return new PrivateChannel(...)) is returning a peculiar error. When I load up the chat page, even though I just tweaked the code to point to a private server, I now get a Error 500 when trying to post to the pusher/auth web.php route. I wish I had more as far as debugging was concerned so I could help narrow down the reasons as to why this might be the case but so far I haven't had any luck with debugging it in the way I would normally debug something. I'll post the code from every relevant file for reference.

// Web.php

Route::post('/pusher/auth', function(){
        return true;
});

// Event File (in App\Events)
...
public function broadcastOn()
    {
        return new PrivateChannel('chatroom.' . $this->chatroomID);
    }
...

// Client-side Code

@extends('layouts.mainpage2')

@section('headincs')
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>Chatroom</title>
    <script>
    window.Laravel = {!! json_encode([
        'chatroom' => $returndata['chatroom']->id,
    ]) !!};
</script>
@endsection

@section('content')
    <style>
        .vh-50 {
            height: 50vh !important;
        }
    </style>
    <script src="https://js.pusher.com/5.0/pusher.min.js"></script>
    <script>

        var pusher = new Pusher('pusherkey', {
            cluster: 'us2',
            forceTLS: true, 
            authEndpoint: '/pusher/auth',
            auth: {
                headers: {
                    'X-CSRF-Token': "{{ csrf_token() }}"
                }
            }
        });

    var channel = pusher.subscribe('private-chatroom.' + window.Laravel.chatroom);
    channel.bind('chatmessagesent', function(data) {
      alert(JSON.stringify(data));
      console.log("received message");
    });

    $(document).ready(function() {
        $('#chatMessageForm').submit(function(event){
            event.preventDefault();
            $.ajax({
                type: 'POST',
                url: '/chatroom/' + window.Laravel.chatroom + '/sendmsg',
                data: $('form#chatMessageForm').serialize(),
                dataType: 'json',
            })
            $('#msgContent').val("");


            return false;
        });
    });
  </script>
    <a href="/userlanding" class="pl-3 pt-3">Back to dashboard</a>
    <div class="container my-5 border border-primary rounded">
        <h3 class="text-center pt-3">{{$returndata['chatroom']->chatroom_name}}</h3>
        <h6 class="text-center pt-2 font-weight-light">Last active at: {{$returndata['chatroom']->last_active_at}}</h6>
        <div class="container my-3 border vh-50">
            <div class="d-flex flex-column-reverse">
            @foreach($returndata['relmsgs'] as $message)
                <div class="w-50">
                    Message Data
                </div>
            @endforeach
            </div>
        </div>
        <div class="row mx-0">
            {!! Form::open(['route' => ['chatroom.sendmessage', 'chatroomID'=>$returndata['chatroom']->id], 'method' => 'POST', 'class' => 'w-100 row mx-0 justify-content-center mb-4', 'id' => 'chatMessageForm']) !!}
                @csrf
                <div class="col-md-10">
                    <textarea class="form-control w-100" name="msgContent" id="msgContent" rows="3"></textarea>
                </div>
                <div class="col-md-2">
                    {{Form::submit('Send Message', ['class' => 'btn btn-primary w-100 h-100'])}}
                </div>
            {!! Form::close() !!}
        </div>
    </div>
@endsection

// Bootstrap.js (in Resources\js)

import Echo from 'laravel-echo';

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

window.Echo = new Echo({
     broadcaster: 'pusher',
     key: 'pusherkey',
     cluster: 'us2',
     forceTLS: true
});

The final line from the bootstrap.js, which is loaded onto the page, is obviously setup for Echo, so I just included it in case there might be some interference. I tried switching over to Echo, thinking perhaps Pusher is better setup for Laravel Echo but I kept getting an error from the line "import Echo from 'laravel-echo'", saying that I couldn't import from a module that wasn't open, so I switched back to this form because I had it working on a public channel earlier today and felt closer to getting it working through this method. Open to any suggestions, thank you.

Nathan
  • 125
  • 13
  • Switch on all error checking and check your log file. – Grumpy Jan 28 '20 at 22:32
  • What lines should I add as error checking? I'm still connected to the websocket even after the 500 error, as typing and submitting a message still submits an event to the error debugging page on pusher, it's just the event isn't picked up by any other clients because it isn't an authorized member of the channel. – Nathan Jan 28 '20 at 22:41
  • Add this to the top of your php file, error_reporting(E_ALL); Run the script and check your error log, it will show you all the indo. – Grumpy Jan 28 '20 at 22:49
  • Got this log record from your help; "local.ERROR: The Response content must be a string or object implementing __toString(), "boolean" given. {"userId":5,"exception":"[object] (UnexpectedValueException(code: 0):" – Nathan Jan 29 '20 at 04:28

1 Answers1

0

Check our what I have done so far here: What can I do to resolve this pusher error-JSON returned from auth endpoint was invalid, yet status code was 200?. While I am yet to fully resolve the issue, I got closer by following your route definition for '/pusher/auth'. I'm no longer getting the invalid json response, I now get a 200 response code. What I now have to figure out is the new error: "Auth info required to subscribe to private-user.3".

Dharman
  • 30,962
  • 25
  • 85
  • 135
Adefowowe
  • 198
  • 2
  • 14
  • I was finally able to resolve the issue I faced. Check it out in my update to the earlier post I referenced. – Adefowowe Jun 11 '21 at 10:24