Websocket Auth Middleware returns Invalid session


#1

I’ve configured everything according to the docs, works without the auth middleware.

auth.js

authenticator: 'session',

socket.js

Ws.channel('chat', 'ChatController').middleware(['auth'])

wsKernel.js

const globalMiddleware = [
  'Adonis/Middleware/Session',
  'Adonis/Middleware/AuthInit'
]

const namedMiddleware = {
  auth: 'Adonis/Middleware/Auth'
}

Ws.registerGlobal(globalMiddleware).registerNamed(namedMiddleware)

Added to app.js > providers

'@adonisjs/websocket/providers/WsProvider'

Would really appreciate some help because I’ll be using Adonis + web-sockets in a production application soon! :smile:


#2

Oh, just found the solution. Thanks Virk!

Used 127.0.0.1 instead of localhost.


#3

I’m having the same issue and I have it setup the exact same way. However, I’m using nginx in my dev env and I’m using a subdomain.

const ws = Ws('ws://example.me') // Works, but auth invalid session
const ws=Ws('ws://app.example.me') // Websocket does not work

I’m guessing this is as @virk said and not working because the websocket connection URL and the Http host have to be the same. Is there a way to include the subdomain into the websocket URL?