thread-sse
Version:
Thread-SSE is a library for Node.js and web browsers to develop security and high-performance SSE (Server-Send-Events) applications.
161 lines (132 loc) • 4.95 kB
JavaScript
/**
* A example of scalable Thread-SSE over HTTP
*
* (c) Copyright 2020-present Richard Li <richard.li@w3plan.net>
* License: MIT
*/
const fs = require('fs');
const path = require('path');
const http = require('http');
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
// Loads custom validation for custom Thread-SSE data
require('./custom/validation');
// Loads custom access control
const { CustomAccessControl } = require('./custom/access-control');
var tsseObj = null;
try {
// Loads Thread-SSE from npm module
require.resolve("thread-sse");
tsseObj = require('thread-sse');
} catch(e) {
// Loads Thread-SSE from local
tsseObj = require('./index');
}
// destructuring assignment
const {
getConnectionPath,
getTsseServicePath,
getsharedDataPath,
libStyleGraph,
responseClient,
setTsseConnection,
tsseServer,
updateTsseData,
updateGroupTsseData
} = { ...tsseObj };
/**
* Clusters Thread-SSE server to each CPU cores
*
*/
if ( cluster.isMaster ) {
console.log('Master ' + process.pid + ' is running');
// Fork workers to your machine
for ( let i = 0; i < numCPUs; i++ ) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log('worker ' + worker.process.pid + ' exited.');
});
} else {
// Sets up sample server
var server = http.createServer (
function (req, res) {
var requestUrl = req.url;
var tsseServicePath = getTsseServicePath(req);
if (requestUrl === "/js/tsse.js")
{
// the default Thread-SSE client library
libStyleGraph(req, res, "/public/js/tsse.js");
} else if (requestUrl === "/client.html")
{
// the sample file of Thread-SSE client
var filePath = path.join(__dirname, "/public/client.html");
try {
var content = fs.readFileSync(filePath, 'utf8');
responseClient(req, res, content);
} catch (er) {
console.log("Server can't read the client file.")
}
} else if (requestUrl === "/js/custom-tsse.js")
{
// the custom Thread-SSE client JavaScript file
libStyleGraph(req, res, "/public/js/custom-tsse.js");
} else if (requestUrl === getConnectionPath())
{
// Handles Thread-SSE connection request
setTsseConnection(req, res);
} else if (requestUrl === tsseServicePath && tsseServicePath !== '')
{
/**
* Applies access control for visitors and logged in users
*
* Controls visitors and logged in users to access tsseServer and updateTsseData
*/
var cac = new CustomAccessControl();
var tsseUser = cac.getTsseUser( req );
// Thread-SSE server for the specific client
tsseServer(tsseServicePath, res);
/**
* Using setTimeout as server event to update Thread-SSE data
*/
// Updates the default Thread-SSE data
setTimeout( function(){
updateTsseData(tsseServicePath, tsseUser, "1000", "Sending Thread SSE Data");
}, 4000 );
// Updates the Thread-SSE data with the same id but different contents
setTimeout( function(){
updateTsseData(tsseServicePath, tsseUser, "1000", "Sending repeated data id");
}, 8000 );
// Updates the custom Thread-SSE data
setTimeout( function(){
updateTsseData(tsseServicePath, tsseUser, "4050", "Joanne Whalley", "(647) 823-7580");
}, 12000 );
// Updates the default Thread-SSE data with defined client action
setTimeout( function(){
updateTsseData( tsseServicePath, tsseUser, "8340", "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js", "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" );
}, 16000 );
// Closes Thread-SSE connection
setTimeout( function(){
updateTsseData(tsseServicePath, tsseUser, "8990");
}, 20000 );
} else if (requestUrl === getsharedDataPath())
{
/**
* Receives shared Thread-SSE data and send it to the members of the group
*
*/
var cac = new CustomAccessControl();
var tsseUser = cac.getTsseUser( req );
var members = cac.getGroupMembersByUser(tsseUser);
updateGroupTsseData(req, res, members);
} else
{
// response Thread-SSE logo
libStyleGraph(req, res, "/public/img/tsse-logo-200x175.jpg");
}
}
);
// Starts sample sever
server.listen(3000, "localhost");
console.log('Worker ' + process.pid + ' started, the app listening at http://localhost:3000');
}