Created
February 14, 2016 20:31
-
-
Save IngwiePhoenix/6befb6b2ce8cc87280cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function Broker(broker) { | |
// Do your Broker setup... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SocketCluster = require("socketcluster"); | |
// Create your SocketCluster instance: | |
var sc = new SocketCluster({ | |
// ... a few other options ... | |
workerCoontroller: require.resolve("./workerController"), | |
brokerCoontroller: require.resolve("./brokerController") | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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