Problems with Webhooks and Websockets on the same port

Viewed 129

I have a server that receives webhooks, sends them to a client via websockets. Since I am listening for both on the same port, if webhooks work, websockets dont and vice versa. How would I go about solving this. Here is the code for the server

const express = require('express');
const app = express();
const bodyParser = require("body-parser");
app.use(bodyParser.json())

app.post("/", (req, res) => {
    console.log(req.body);
    res.status(200);
    res.send(req.body);
    echo(req.body);
});

app.listen(3000, '0.0.0.0', () => console.log('Server is live at 3000'));

const WebSocket = require('ws');
const SocketServer = require('ws').Server;

const server = express().listen(3000);

const wss = new SocketServer({server});

wss.on('connection', (ws) =>{
    console.log("Connected");
    ws.send("Connected To Server")
    ws.on('close', () => console.log("Disconnected"));
});

function echo(webhook){
    wss.clients.forEach(function each(client){
        client.send(webhook);
    });
}
  1. How would I run webhook listener and websocket sender/listener on same port

  2. Can I run both of them on different ports on the same url

    a) If yes what would be the url for both ports, if my website is www.example.com

  3. If all that is not possible, what would be a good alternative

0 Answers
Related