1

I am using Angular8 as frontend and Nodejs as backend

I have Configured WSS on production ,but connection with client not working properly, In one page connection is working but in another page connection not working.

websocket and server is running on same port

Everything working fine with Ws(localhost)

package we are use in backend

https://www.npmjs.com/package/ws

Back-end code:

express = require('express');
app = express();
const http = require('http');
const port = 8080;
const fs = require('fs');
const certificate = {
 cert: fs.readFileSync(''),
 key: fs.readFileSync('')
}
const httpServer = http.createServer(app,certificate);

var server = require('ws');

var s =  new server.Server({ server:httpServer },{
  rejectUnauthorized: false
});
s.on('connection', function (ws) {
    ws.on('message', function (message) {
         var obj = JSON.parse(message);
          if (obj.messagetype == "test") {
              //send data
            }     
    }
}

httpServer.listen(port);

Front-end code

export class WebsocketService {
constructor() { }
private subject: Rx.Subject<MessageEvent>;

public connect(url): Rx.Subject<MessageEvent> {
  if (!this.subject) {
    this.subject = this.create(url);
  }
 return this.subject;
}

private create(url): Rx.Subject<MessageEvent> {
  let ws = new WebSocket(url);

  let observable = Rx.Observable.create((obs: Rx.Observer<MessageEvent>) => {
   ws.onmessage = obs.next.bind(obs);
   ws.onerror = obs.error.bind(obs);
   ws.onclose = obs.complete.bind(obs);
 return ws.close.bind(ws);
});
  let observer = {
  next: (data: Object) => {
   if (ws.readyState === WebSocket.OPEN) {
      ws.send(JSON.stringify(data));
    }
  }
 };
  return Rx.Subject.create(observer, observable);
  }
 }

const CHAT_URL = "wss://cen.abcuae.com/";

export class SockoneService {

public messages: Subject<Message>;
public messages2: Subject<Message>;

constructor(wsService: WebsocketService) {
 this.messages = <Subject<Message>>wsService.connect(CHAT_URL).pipe(map(
   (response: MessageEvent): Message => {
     let data = JSON.parse(response.data);
     return data;
   }));
   this.messages2 =  this.messages;
 }

here is configuration under /etc/nginx/sites-available/default

server {

listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/cen.abcuae.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/cen.abcuae.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

server_name cen.abcuae.com;

location / {
    
            proxy_pass http://0.0.0.0:8080; #whatever port your app runs on
            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_read_timeout 3600; 

} 
}
server {
listen 443;
listen [::]:443 ssl;

 server_name cen.abcuae.com;
    ssl_certificate /etc/letsencrypt/live/cen.abcuae.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/cen.abcuae.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by C


location /websocket {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  
  proxy_set_header Host $host;
  proxy_pass https://cen.abcuae.com:5000;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
}
}

1 Answers1

0
express = require('express');
app = express();

//Use Here https
const http = require('https');

const port = 8080;
const fs = require('fs');
const certificate = {
 cert: fs.readFileSync(''),
 key: fs.readFileSync('')
}
const httpServer = http.createServer(app,certificate);

var server = require('ws');

var s =  new server.Server({ server:httpServer },{
  rejectUnauthorized: false
});
s.on('connection', function (ws) {
    ws.on('message', function (message) {
         var obj = JSON.parse(message);
          if (obj.messagetype == "test") {
              //send data
            }     
    }
}
Amir Jani
  • 103
  • 4