Search code examples
laravelvue.jssocket.ionuxt.jslaravel-echo

Socket IO client should be globally available error


I am using nuxt/laravel api with laravel-echo-server and socket.io-client.

When I use it this way:

import Echo from 'laravel-echo'
if (process.client) {
  window.io = require('socket.io-client')
  window.Echo = new Echo({
     broadcaster: 'socket.io',
     host: 'http://seekers-backend.com:6001',
     auth: { headers: { Authorization: 'Bearer-token' } }
  })
}

And then use window.Echo.private().listen().... it works perfectly..

But when I try to declare it within data property I get error because that is rendered on server side... How can I use socket.io-client on server side rendering or how can I declare it on client side since I need access to getters for Bearer token.


Solution

  • I guess I found the way that works for unknown reason :D

    import Echo from 'laravel-echo'

    if (process.client) {
      window.io = require('socket.io-client')
    }
    

    And then

    mounted() {
      let echo = new Echo({
        broadcaster: 'socket.io',
        host: 'http://seekers-backend.com:6001',
        auth: { headers: { Authorization: this.token } }
      })
      echo.private('user.' + this.$auth.user.id)
      .listen('MessageEvent', (e) => {
        // Do stuff
      })
    }