Synchronize your redux storage in a cluster.
- Supports native methods of redux.
- Uses IPC only (in Basic Scheme) or IPC and Socket (in Cluster Scheme).
- Store are isolated and identified by means of hashes.
npm i redux-cluster --save
var ReduxCluster = require('redux-cluster');
var Test = ReduxCluster.createStore(editProcessStorage);
function editProcessStorage(state = {version:''}, action){
switch (action.type){
case 'TASK':
var state_new = {};
state_new.version = action.payload.version;
return state_new;
break;
default:
break;
}
}
Test.subscribe(function(){
if(Cluster.isMaster){
var name = 'm';
} else {
var name = Cluster.worker.id;
}
console.log(Colors.gray(name + ' | ' + JSON.stringify(Test.getState())));
});
Test.dispatch({type:'TASK', payload: {version:'1111111'}})
Default is console.error
Test.stderr = function(err){console.error(err);}
This mode is enabled for the basic scheme as well.
Set the type of synchronization, the default action
.
- action - send a action of the store status for each action and send a snapshot every 1000 (default, Test.resync) action
- snapshot - send a snapshot of the store status for each action
Test.mode = "action";
Number of actions before a snapshot of guaranteed synchronization will be sent. Default 1000 actions.
Test.resync = 1000;
Please familiarize yourself with the architectural schemes before use. In Windows createServer is not supported in child process (named channel write is not supported in the child process), please use as TCP-server.
Test.createServer(<Options>);
var Test = ReduxCluster.createStore(reducer);
Test.createServer({path: "./mysock.sock", logins:{test1:'12345'}});
var Test2 = ReduxCluster.createStore(reducer2);
Test2.createServer({host: "0.0.0.0", port: 8888, logins:{test2:'123456'}});
Options Required:
- path - name of the file socket (linux) or the name of the named channel (windows), if use as IPC
- host - hostname or ip-address (optional, default 0.0.0.0), if use as TCP
- port - port (optional, default 10001), if use as TCP
- logins - login - password pairs as
{login1:password1, login2:password2}
.Test.createClient(<Options>);
var Test = ReduxCluster.createStore(reducer); Test.createClient({path: "./mysock.sock", login:"test1", password:'12345'}); var Test2 = ReduxCluster.createStore(reducer2); Test2.createClient({host: "localhost", port: 8888, login:"test2", password:'123456'});
Options Required:
- path - name of the file socket (linux, file will be overwritten!) or the name of the named channel (windows), if use as IPC
- host - hostname or ip-address (optional, default 0.0.0.0), if use as TCP
- port - port (optional, default 10001), if use as TCP
- login - login in socket
- password - password in socket
return true if connected, false if disconnected
Test.connected;
return role:
- master (if Master process in Cluster, sends and listen action to Worker)
- worker (if Worker process in Cluster, processes and sends action to Master)
- server (if use
createServer(<Object>)
, sends and listen action to Client) - client (if use
createClient(<Object>)
, processes and sends action to Server)
Test.role;
npm i redux-cluster-ws --save
require('redux-cluster-ws').server(Test); Test.createWSServer(<Options>);
require('redux-cluster-ws').server(Test); Test.createWSServer({ host: "0.0.0.0", port: 8888, logins:{ test2:'123456' }, ssl:{ key: /path/to/certificate-key, crt: /path/to/certificate, ca: /path/to/certificate-ca } }); require('redux-cluster-ws').server(Test2); Test2.createWSServer({ host: "localhost", port: 8889, logins:{ test2:'123456' } });
Options Required:
- host - hostname or ip-address
- port - port (optional, default 10002)
- logins - login - password pairs as
{login1:password1, login2:password2}
. - ssl - path to server certificate (if use as https, default use http).
Client does not use internal Node libraries for webpack compatibility. Therefore, on the client, you must create a store with the same reducer.
//create Redux Store var ReduxClusterWS = require('redux-cluster-ws').client; var Test = ReduxClusterWS.createStore(<Reducer>); //connect to Redux-Cluster server (use socket.io) Test.createWSClient(<Options>);
var Test = ReduxCluster.createStore(reducer); Test.createWSClient({host: "https://localhost", port: 8888, login:"test2", password:'123456'});
Options Required:
- host - hostname or ip-address (protocol include)
- port - port (optional, default 10002)
- login - login in websocket
- password - password in websocket
Save storage to disk and boot at startup. It is recommended to perform these actions only in the primary server / master, since they create a load on the file system.
Attention! For Worker and Master, you must specify different paths. Return Promise object.Test.backup(<Object>);
Test.backup({ path:'./test.backup', key:"password-for-encrypter", count:1000 }).catch(function(err){ ... you handler });
Options Required:
- path - file system path for backup (Attention! File will be overwritten!)
- key - encryption key (can be omitted)
- timeout - backup timeout (time in seconds for which data can be lost), if count is omitted.
- count - amount of action you can lose
You can use
createServer(<Object>)
in any process in cluster (and outside cluster process).
UsingcreateClient(<Object>)
is logical in a Master process or a single process. In any case, if you create acreateClient(<Object>)
in the Worker process, it will not work with the rest of the cluster processes, does not have access to them. So you will have to createcreateClient(<Object>)
in each Worker process that needs access to the Store.This is a bad way, it will lead to breaks in the interaction of the ReduxCluster with the Master process.
var ReduxCluster = require('redux-cluster'), Cluster = require('cluster'), Lodash = require('lodash'); var Test = ReduxCluster.createStore(editProcessStorage); function editProcessStorage(state = {version:''}, action){ try { switch (action.type){ case 'TASK': var state_new = Lodash.clone(state); state_new.version = action.payload.version; return state_new; break; default: break; } } catch(e){ } var state_new = Lodash.clone(state); return state_new; } Test.subscribe(function(){ if(Cluster.isMaster){ var name = 'm'; } else { var name = Cluster.worker.id; } console.log(name + ' | ' + JSON.stringify(Test.getState())); }); if(Cluster.isMaster){ for(var i=0; i < 3; i++){ setTimeout(function(){Cluster.fork();}, i*10000) } Test.dispatch({type:'TASK', payload: {version:'MasterTest'}}); } else { Test.dispatch({type:'TASK', payload: {version:'WorkerTest'+Cluster.worker.id}}); }
var ReduxCluster = require('redux-cluster'), Cluster = require('cluster'), Lodash = require('lodash'); var Test = ReduxCluster.createStore(editProcessStorage); if(Cluster.isMaster){ Test.createServer({path: "./mysock.sock", logins:{test1:'12345'}}); } function editProcessStorage(state = {version:''}, action){ try { switch (action.type){ case 'TASK': var state_new = Lodash.clone(state); state_new.version = action.payload.version; return state_new; break; default: break; } } catch(e){ } var state_new = Lodash.clone(state); return state_new; } Test.subscribe(function(){ if(Cluster.isMaster){ var name = 'm'; } else { var name = Cluster.worker.id; } console.log(' S1 | ' + name + ' | ' + JSON.stringify(Test.getState())); }); if(Cluster.isMaster){ for(var i=0; i < 1; i++){ setTimeout(function(){Cluster.fork();}, i*10000); } var i = 0; setInterval(function(){ Test.dispatch({type:'TASK', payload: {version:'MasterTest'+i}}); i++; }, 19000); } else { var i = 0; setInterval(function(){ Test.dispatch({type:'TASK', payload: {version:'WorkerTest'+i}}); i++; }, 31000+(Cluster.worker.id*3600), i); }
var ReduxCluster = require('redux-cluster'), Cluster = require('cluster'), Lodash = require('lodash'); var Test = ReduxCluster.createStore(editProcessStorage); if(Cluster.isMaster){ Test.createClient({path: "./mysock.sock", login:"test1", password:'12345'}); } function editProcessStorage(state = {version:''}, action){ try { switch (action.type){ case 'TASK': var state_new = Lodash.clone(state); state_new.version = action.payload.version; return state_new; break; default: break; } } catch(e){ } var state_new = Lodash.clone(state); return state_new; } Test.subscribe(function(){ if(Cluster.isMaster){ var name = 'm'; } else { var name = Cluster.worker.id; } console.log(name + ' | ' + JSON.stringify(Test.getState())); }); if(Cluster.isMaster){ for(var i=0; i < 2; i++){ setTimeout(function(){Cluster.fork();}, i*8000); } var i = 0; setInterval(function(){ Test.dispatch({type:'TASK', payload: {version:'OneRemoteMasterTest'+i}}); i++; }, 11000); } else { var i = 0; setInterval(function(){ Test.dispatch({type:'TASK', payload: {version:'OneRemoteWorkerTest'+i}}); i++; }, 22000+(Cluster.worker.id*1500), i); }
Encryption (crypto.createCipheriv) and Decryption (crypto.createDecipheriv) features have been marked as deprecated in Node v12. I updated these functions in version 1.7.0, but if you used a backup of the storage to disk, you will not be able to download it.
MIT