Running Socket.io between Android and Nodejs (behind NGINX with https)

Viewed 69

I'm attempting to make a real-time location traction feature for my kotlin based android application. The backend is a NodeJS server behind an NGINX proxy with https. I've been testing the connection with firefox for now just to see if I can get the socket to connect but regardless of what I do, the result is 'false'. I've attached some of the relevant code below. I'm simulating the client connection in nodejs for now since my friend is the one working on the android side of things. In the console I see that the line console.log(connected:${socket.connected}) is printing true but the response is false(as seen in the browser or android app). Any idea what could be going on? Please let me know if I should provide more information. Thanks!

NodeJS(simulated client):

const ioClient = require('socket.io-client')
---snip---
app.get('/socket.io', (req, res) => {
  const socket = ioClient('http://0.0.0.0:3000', {
    path: '/socket.io',
  })
  // const socket = io.connect('http://0.0.0.0:3000')
  socket.on('hello', (args) => {
    console.log(args)
  })
  socket.on('connect', (args) => {
    console.log(`connected:${socket.connected}`)
  })
  //console.log(socket)
  //res.send(`testing socket:\n ${util.inspect(socket, { depth: null })}`)
  res.send(socket.connected)
})

NodeJS(Server)

const server = require('http').createServer(app)
const io = require('socket.io')(server, {
  path: '/socket.io',
})

---snip---

io.on('connection', (socket) => {
  console.log('a user has connected')
  socket.emit('hello', 'world')
  //socket.on('new location')
})
io.on('hello', (socket) => {
  console.log(`connected in hello`)
})

NGINX

 location /socket.io {
        # proxy_pass http://nodejs:3000/socket.io;
        # proxy_http_version 1.1;
        # proxy_set_header Upgrade $http_upgrade;
        # proxy_set_header Connection 'upgrade';
        # proxy_set_header Host $host;
        # proxy_cache_bypass $http_upgrade;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy false;
        
        proxy_pass http://localhost:3000;
        proxy_redirect off;
    
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
  }
1 Answers

Have you properly setup all conten security policies? I have solved similar problem on Safari on iPad. I use this configuration in Express, you can do similarly on nginx.

app.use(
  helmet({
    contentSecurityPolicy: {
      directives: {
        defaultSrc: ["'self'"],
        blockAllMixedContent: [],
        fontSrc: ["'self'", 'https:', 'data:'],
        frameAncestors: ["'self'", 'https://accounts.google.com/'],
        frameSrc: ["'self'", 'https://accounts.google.com/'],
        imgSrc: ["'self'", 'data:'],
        objectSrc: ["'self'", 'blob:'],
        mediaSrc: ["'self'", 'blob:', 'data:'],
        scriptSrc: ["'self'", 'https://apis.google.com'],
        scriptSrcAttr: ["'none'"],
        styleSrc: ["'self'", 'https:', "'unsafe-inline'"],
        upgradeInsecureRequests: [],
        connectSrc: ["'self'", 'wss://your_socket_address.com'],
      },
    },
  })
);
Related