Websocket subdomain - Auth Invalid session


#1

I’m trying to get middleware auth working with websocket and I have it working, but it doesn’t work when I use subdomains. For instance:

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

I’ve configured it exactly how the docs describe. Does anyone know what might be causing this and if there’s any way around it?

Thanks


#2

There are handful of unknowns here.

  1. Which auth scheme you are using.
  2. At which domain does the user login?
  3. Are your cookies shared between subdomains (if using session scheme)?

#3

@virk

  1. Which auth scheme you are using. Using sessions
  2. At which domain does the user login? The user logs in at app.example.me
  3. Are your cookies shared between subdomains (if using session scheme)? No, but the dev env only uses the subdomain.

The error that I’m getting when using (“ws://app.example.me”) is:

WebSocket connection to ‘ws://app.example.me/adonis-ws’ failed: Error during WebSocket handshake: Unexpected response code: 502


#4

Do you have nginx proxy in front of your app? Can you please the file contents too?


#5

The nginx proxy is in front of the app.

Webpack dev server is running the SPA (dev) on port 8081.

Adonis server is running on port 4000.

webpack.config.js

      proxy: {
                "/app":{
	            target: 'http://127.0.0.1:4000',
                    pathRewrite: {"^/app" : ""}
                }
            },

auth.js

authenticator: 'session',

Added to app.js > providers

'@adonisjs/websocket/providers/WsProvider'

nginx.conf

server {
  listen 80;
  expires $expires;
  server_name app.example.me;

  location / {
      proxy_pass http://localhost:8081;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_cache_bypass $http_upgrade;
  }

    # catches all ajax requests
    location /* {
      proxy_pass http://localhost:4000;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_cache_bypass $http_upgrade;
  }
}

wsKernal.js

const Ws = use('Ws')

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

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

Ws.registerGlobal(globalMiddleware).registerNamed(namedMiddleware)

start/socker.js

const Ws = use('Ws')
Ws.channel('chat', 'ChatController').middleware(['auth']);

clientApp/models/socket.js*

import "@babel/polyfill";
import Ws from "@adonisjs/websocket-client";

const ws = Ws('ws://app.example.me')
...

I believe this is everything.