Same code working at server but not at local. Though the addEventListener is adding the open event but onopen event is not triggered. Both my local and live server is nginx. Both local and live php version is php7-fpm. The only difference is my server is configured for https but my local is http.
my javascript code:
'use strict';
var servers = {
iceServers: [
{urls: 'stun:stun.l.google.com:19302'}
]
};
var myPC;
var awaitingResponse;
var streamConstraints;
var myMediaStream;
const room = getRoom();
//const wsChat = new WebSocket("wss://localhost/websocket"); //this is for server which is HTTPS
const wsChat = new WebSocket("ws://localhost:8090");//localhost which is HTTP
wsChat.addEventListener('open', function (event) {
console.log('open event listener');//getting this on console
});
wsChat.addEventListener('message', function (event) {
console.log('Message event listener');
});
window.addEventListener('load', function(){
console.log(wsChat);
wsChat.onopen = function(event){
alert('connected'); //not getting this alert
wsChat.send(JSON.stringify({
action: 'subscribe',
room: room
}));
showSnackBar("Connected to the chat server!", 5000);
};
wsChat.onerror = function(event){
showSnackBar("Unable to connect to the chat server! Kindly refresh", 10000);
};
wsChat.onmessage = function(e){
var data = JSON.parse(e.data);
console.log(data);
if(data.room === room){
switch(data.action){
.....
}
}
};
});
The nginx configuration of my server and my local.
Server: mysite.conf
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server 127.0.0.1:8090;
}
......
location = /websocket{
proxy_redirect off;
proxy_pass http://websocket;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 3600;
}
This configuration is working at my live site.
Local nginx conf:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server 127.0.0.1:8090;
}
......
location = /websocket {
proxy_redirect off;
proxy_pass http://websocket;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_read_timeout 3600;
}
The response what I'm getting at my console for local.
Not getting any clue.
</div>