I have an express app on a single server running behind nginx
I've tested socket.io communication and it runs fine on our dev server.
Adding ssl to the mix yielded the following console log on the client:
As you can notice it produces a 400 (Bad Request) - and then loads the same request successfully, but my console logs are not being run since the connection is not successful...
Would appreciate any feedback. Thanks.
This is the code:
This is my nginx config:
upstream example.com {
server 127.0.0.1:3000;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/nginx/ssl/example.crt;
ssl_certificate_key /path/to/nginx/ssl/example.key;
location / {
proxy_pass http://example.com;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr ;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
proxy_set_header X-Forwarded-Proto https;
}
location /public {
root /var/www/example/;
}
location /favicon.ico {
root /var/www/example/public/favicon.ico;
}
}
server {
listen 443 ssl;
server_name www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 80;
server_name example.com;
return 301 https://example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
return 301 https://example.com$request_uri;
}
this is my socket.io related code on the express server
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
http.listen(port, function(){
console.log('MyDomain.com app listening at ', 'http://localhost:'+port);
io.on('connection', function(socket){
console.log('socket.io connected','socket.id: '+socket.id);
socket.on('new-user', function(data) {
var user_id = data.user_id;
console.log('new-user, user_id: ', user_id);
});
});
});
this is my client side that tries to connect:
var socket = io.connect(null, { secure: true, port: 443, rememberTransport: false, 'reopen delay': 1000 });
//I have also tried these variations...
//-----------------------------------------
//var socket = io();
//var socket = io.connect('https://localhost', {secure: true});
//var socket = io.connect('https://example.com',{secure: true});
//var socket = io.connect('https://example.com');
//-----------------------------------------
socket.on('connect', function () {
console.log('on "connect"');
socket.emit('new-user', {user_id:some_user_id});
});
on my html template I'm using this to load socket.io
<script src="/socket.io/socket.io.js"></script>
UPDATE 01: I've noticed that when I go to a different URL in the browser, then come back - the sockets are loading correctly... Does this indicate a delay which is needed...?