This script, works, sort of, but randomly crashes node:
Uses several web servers, one to accept all requests, send static
files, and two more for proxied requests (/handler -> 8080, rest go to
socket.io) - curiously this seems to be the only combination that
works for me.
---------------------------------------------
var express = require('express');
const redis = require('redis');
const client = redis.createClient();
const io = require('
socket.io');
var http = require ('http')
var httpProxy = require ('http-proxy')
http.createServer (function (req, res) {
var proxy = new httpProxy.HttpProxy (req, res)
console.log (req.url)
if (req.url.indexOf ('/handler') > -1)
{
proxy.proxyRequest (8080, 'localhost', req, res)
}
else
{
proxy.proxyRequest (3000, 'localhost', req, res)
}
}).listen (7000)
var app = module.exports = express.createServer();
app.use(express.staticProvider(__dirname + '/public'));
if (!module.parent) {
app.listen (3000, 'localhost');
console.log ("Express server listening on port %d", app.address
().port)
const socket = io.listen (app);
socket.on ('connection', function (client) {
const subscribe = redis.createClient ();
subscribe.subscribe('pubsub');
subscribe.on ("message", function (channel, message) {
client.send(message);
});
client.on('message', function (msg) {
console.log (msg)
});
client.on('disconnect', function () {
subscribe.quit ();
});
});