I have a problem, I am mounting an express server with Sockets (module socketcluster-server) but when I am sending http requests the express is blocked after about 20 requests, causing the Sockets (client) to notify that they have run out Connection.
Has someone happened to you? Any ideas that can help me solve this problem?
Thank you
express.js
const http = require('http'),
express = require('express'),
socket = require('./socket'),
HOST = 'localhost',
PORT = 8000,
bodyParser = require('body-parser');
const app = express()
const httpServer = http.createServer(app);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/',(req,res)=>res.send({status:"ok"}))
socket.init(httpServer,HOST,PORT)
httpServer.listen(PORT,HOST);
httpServer.on('listening', () =>{
console.log('Express server started on port %s at %s', httpServer.address().port, httpServer.address().address);
});
socket.js
const socketClusterServer = require('socketcluster-server')
module.exports = (() => {
let scServer
const createInstance = (httpServer, host, port) => {
if (!scServer)
scServer = socketClusterServer.attach(httpServer, {host, port});
scServer.on('connection', function (socket) {
console.log('socker: ', scServer.clientsCount)
socket.on('client',getMessageByClient)
})
}
const getMessageByClient = (data) => {
if (data.channel)
sendMessage(data.channel, data)
scServer.removeListener('client', getMessageByClient)
}
const sendMessage = (channel, message) => scServer.exchange.publish(channel, JSON.stringify(message))
const close = () => scServer.close()
return {
init: (httpServer, host, port) => createInstance(httpServer, host, port),
sendMessage: (channel, message) => sendMessage(channel, message),
close: () => close()
}
})()