Search code examples
javascriptmultipartform-dataform-datalaravel-echo

How to add form-data to Laravel Echo request?


I'm trying to send some data with Laravel Echo request

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'somekey',
    wsHost: '127.0.0.1',
    wsPort: 6001,
    encrypted: false,
    disableStats: true,
    forceTLS: false,
    authEndpoint: 'http://127.0.0.1:8000/broadcasting/auth',

    'form-data': {          // I tried data, dataForm, dataform
        someData: '123',    // this doesn't seem to work                
    },                  
});

I've seen how to add custom headers to the request

auth: {
    headers: {
          token: '123'
    }
}

Is there any way to add form-data in a similar way?

Edit

When I inspect the network requests in the DevTools, I can see that there are two formData properties sent by the Echo to the server. So I thought there must be a way to attach additional properties into the existing formData object


Solution

  • Is there any way to add form-data in a similar way?

    The simple answer is - NO

    Laravel Echo doesn't have the functionality to achieve that within the parameter set.

    The reason we can see the Form Data object in the Dev Tools requests, is because pusher-js is adding them before making the request to the server. To achieve that, we would have to manipulate the pusher API before the request is executed, but this goes off the original topic of this thread.

    So if you want to send the data to the server, the easiest would be adding custom headers as pointed in the original question.

    ...
    auth: {
        headers: {
              token: '123'
        }
    }
    ...