NodeJS Websockets with Traefiks

Discussão em 'Angular' iniciado por user17293718, Dezembro 16, 2024.

  1. user17293718

    user17293718 Guest

    i do have a Angular App which uses Websockets (in dev environment, everything works, also for the WS stuff) together with Node JS. Now i do want to put all of this behind a traefiks, but i do get a 504, in case i want to use the websocket service. I have changed the URL also in "https://my-domain.de:8080", but none of these tries were successfull:

    this.socket = io("https://backend.my-domain.de:8080");


    backend is a subdomain, which is working for all HTTP pathes, that are available on my node JS express server. So if i call "https://backend.my-domain.de/getAllUsers" would work perfectly fine with this. However ontop of this server i create the WS:

    (async () => {
    try {
    const app = express();
    const API = await new APIconfig(`${__dirname}/config/APIconfig.json`).getConfig();
    mongoose.connect(API.MONGODB_PRODENVIROMENT_API);
    const serverInstance = server.createServer(app);
    fromEvent(serverInstance.listen(API.PORT, () => console.log(`Server is listening on 'http://localhost:${API.PORT}`)), 'listening')
    .pipe(
    tap(new CorsConfiguration(app)),
    tap(new httpRoutesConfiguration(app)),
    tap(new WebsocketServerConfiguration(serverInstance)),
    )
    } catch (ex) {
    logger.error(ex);


    })();

    The websocket server looks like that:

    module.exports = class WebsocketServerConfiguration {

    // broadcast to all users. Check for client.connected=true property
    socketConnections = [];

    constructor(server) {
    this.io = socket(server, { cors: { origin: '*' } });
    this.socket = this.io.on('connection', (socket) => {return socket });
    // this.eventController = new eventController();
    this.registerOnConnection();
    }


    }

    So the port for the node js server (serverInstance on my example) listen to Port 8080. Every call to this server uses a subdomain "backend" like this:

    services:
    backend:
    image: node:latest
    container_name: nodejs-backend
    command: node /usr/src/app/index.js
    volumes:
    - ./dist:/usr/src/app
    - ./img:/usr/src/app/img
    networks:
    - default
    - web
    labels:
    - traefik.enable=true
    - traefik.http.routers.backend.rule=Host(`backend.my-domain.de`)
    - traefik.http.routers.backend.tls=true
    - traefik.http.routers.backend.tls.certresolver=lets-encrypt
    - traefik.http.services.backend.loadbalancer.server.port=8080


    networks: web: external: true

    So this example works on prod for any regular API call, but not for WS. Can you give me a hint, how to solve this problem? The Traefiks logs were not shown any helpful information. Do i need to build up another server instance just for WS, which uses a different Port then the main backend? I do not know if both can run on 8080, which i did for development. Thanks guys

    Continue reading...

Compartilhe esta Página