Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only users who are on the same network can communicate on socket.io #708

Open
MoisesSousa opened this issue Jul 4, 2022 · 0 comments
Open

Comments

@MoisesSousa
Copy link

I'm using nginx on linux to put my nodejs and socket.io signaler server online, I don't know why but only users who are on the same network can communicate on socket.io, how can i configure nginx and socket.io server to allow communication between users outside the network ?

Here is my nginx and socket.io server configuration:

  1. /etc/nginx/conf.d/app.com.conf

server {
listen 80;
listen [::]:80;
server_name 5902483.lovecames.com;
root /usr/share/nginx/html;
return 301 https://$host$request_uri;
}

server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name 5902483.lovecames.com;
root /usr/share/nginx/html;

ssl_certificate /...;
ssl_certificate_key /...;
ssl_session_timeout ...;
ssl_session_cache ...;
ssl_session_tickets off;

# intermediate configuration
ssl_protocols ...;
ssl_ciphers ...
ssl_prefer_server_ciphers off;

# HSTS (ngx_http_headers_module is required) (63072000 seconds)
add_header Strict-Transport-Security "max-age=63072000" always;

resolver 0.0.0.0;

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

location ~* .io {
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:8080;
  proxy_redirect off;

  proxy_http_version 1.1;

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}

location ~* / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}

error_page 404 /404.html;
location = /404.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

  1. /etc/nginx/nginx.conf

include /etc/nginx/conf.d/*.conf;

server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;

  # Load configuration files for the default server block.
  include /etc/nginx/default.d/*.conf;
  error_page 404 /404.html;
  location = /404.html {
  }

  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
  }

}

  1. nodejs server:

var fs = require('fs');

var _static = require('node-static');
var file = new _static.Server('./static', {
cache: false
});

var app = require('http').createServer(serverCallback);

function serverCallback(request, response) {
request.addListener('end', function () {
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
response.setHeader('Access-Control-Allow-Headers', 'Content-Type');

    file.serve(request, response);
}).resume();

}

var io = require('socket.io').listen(app, {
// log: true,
origins: ':'
});

io.set('transports', [
//'websocket',
'xhr-polling',
'jsonp-polling'
]);

var channels = {};
var users = [];
io.sockets.on('connection', function (socket) {
var initiatorChannel = '';
if (!io.isConnected) {
io.isConnected = true;
}

socket.on('new-channel', function (data) {
    if (!channels[data.channel]) {
        initiatorChannel = data.channel;
    }

    channels[data.channel] = data.channel;
    onNewNamespace(data.channel, data.sender);
});

socket.on('presence', function (channel) {
    var isChannelPresent = !! channels[channel];
    socket.emit('presence', isChannelPresent);
});

socket.on('disconnect', function (channel) {
    if (initiatorChannel) {
        delete channels[initiatorChannel];
    }
});

});

function onNewNamespace(channel, sender) {
io.of('/' + channel).on('connection', function (socket) {
var username;
if (io.isConnected) {
io.isConnected = false;
socket.emit('connect', true);
socket.emit('user-video-stream', JSON.stringify(users));
}

    socket.on('message', function (data) {
        if (data.sender == sender) {
            if(!username) username = data.data.sender;
            
            socket.broadcast.emit('message', data.data);
        }
    });
    
    socket.on('disconnect', function() {
        if(username) {
            socket.broadcast.emit('user-left', username);
            username = null;
        }
    });
});

}
function isInArray(users,newUser) {
found = false;
users.forEach(function(element) {
if(element.videoId == newUser.videoId) found = true;
}, this);
return found;
}
app.listen(8080, '0.0.0.0', function(){
console.log('listening on *:8080');
});

  1. client-side:

var SIGNALING_SERVER = 'https://5902483.lovecames.com/';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant