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

Kris01's avatar

(in promise) SyntaxError: "[object Object]" is not valid JSON

I'm trying to use laravel websocket beyondcode without pusher

this is my authentication route

Route::post('/broadcasting/auth', function (Request $request) {
    if (Auth::check()) {
        $socket_id = $request->socket_id;
        $channel_name = $request->channel_name;

        $user_id = Auth::user()->id;
        $user_name = Auth::user()->name;

        $data = [
            'user_id' => $user_id,
            'user_name' => $user_name
        ];

        $signature = hash_hmac('sha256', $socket_id.':'.$channel_name.':'.json_encode($data), env('APP_KEY'));
        Log::debug('if');
        return response()->json([
            'channel_data' => [
                'user_id' => $user_id,
                'user_name' => $user_name,
                'signature' => $signature
            ]
        ]);
    } else {
        Log::debug('else');
        return response()->json([], 403);
    }
});

this is my echo

    Echo.join('chat').subscribed(() => {
            console.log('subscribed') // it shows subscribed on the console
        }).error((error) => {
            console.log('ERROR: ', error)
        }).listen('.message.sent', (event) => {
            console.log('event: ')
        })

And I am getting in my browser console

(in promise) SyntaxError: "[object Object]" is not valid JSON
0 likes
2 replies
LaryAI's avatar
Level 58

The error message suggests that the response from the authentication route is not valid JSON. One possible reason for this could be that the response is not being returned as JSON, or that there is an error in the JSON encoding.

To debug this issue, you can try logging the response from the authentication route to see what it contains. You can also try using a tool like Postman to send a request to the authentication route and inspect the response.

Assuming that the response is indeed invalid JSON, one possible solution is to ensure that the response is returned as valid JSON. In the code provided, the response is being returned as an array, which is then converted to JSON using the response()->json() method. However, this method expects an array or an object as input, and not a string.

To fix this, you can try returning the response as an object instead of an array, like this:

return response()->json([
    'channel_data' => [
        'user_id' => $user_id,
        'user_name' => $user_name,
        'signature' => $signature
    ]
]);

Alternatively, you can try encoding the array as JSON manually using the json_encode() function, like this:

return response(json_encode([
    'channel_data' => [
        'user_id' => $user_id,
        'user_name' => $user_name,
        'signature' => $signature
    ]
]))->header('Content-Type', 'application/json');

This should ensure that the response is returned as valid JSON, which should resolve the error.

stevenbauman's avatar

Make sure your BROADCAST_DRIVER is set to pusher. I received your same error if I set it to redis.

Please or to participate in this conversation.