Skip to content

Instantly share code, notes, and snippets.

@IngwiePhoenix
Created February 14, 2016 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IngwiePhoenix/6befb6b2ce8cc87280cb to your computer and use it in GitHub Desktop.
Save IngwiePhoenix/6befb6b2ce8cc87280cb to your computer and use it in GitHub Desktop.
module.exports = function Broker(broker) {
// Do your Broker setup...
}
var SocketCluster = require("socketcluster");
// Create your SocketCluster instance:
var sc = new SocketCluster({
// ... a few other options ...
workerCoontroller: require.resolve("./workerController"),
brokerCoontroller: require.resolve("./brokerController")
});
module.exports = function Middlewares(app, sc) {
// /user route...
app.use("/user/login", function(req, res, next){
if(req.method.toLowerCase() == "post") {
// Post logic.
// The following is pseudo-y code, and just to demonstrate.
// @var redis : Redis client
// @var data : The data we'd send back
redis.get((...), function(res, err){
if(!! res) {
// The record is found, aka. we logged the user in.
res.satus(200)
.write(data)
.end();
// We can use the sc variable to interact with the SocketCluster server.
// For instance, assuming that .broadcast() would send to all users,
// let's telle veryone that this specific user logged in!
// The "..." is just to omit the arguments for this demo.
sc.broadcast(...)
}
});
}
});
// Use a static middleware, like "st".
app.get("/", require("st")());
}
module.exports = function Worker(worker) {
var app = require("express")();
// Get a reference to our raw Node HTTP server
var httpServer = worker.getHTTPServer();
// Get a reference to our realtime SocketCluster server
var scServer = worker.getSCServer();
// Tell the HTTP Server to handle express:
httpServer.on('request', app);
// Attach middlewares
require("./middlewares")(app, scServer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment