When Neo4j 3.1 was introduced, it also came along with a new clustering protocol, bolt+routing://. We believe that the new casual cluster protocol is superior to the high availability one and we switched to the new official driver from neo4j (found here), thus deprecating this one.
If you are still using this driver wrapper, we are happy to receive PR's to make it better.
Neo4j cluster session and transparent failover
This driver is a wrapper over the official Neo4j bolt driver and adds a layer of High Availability and transparent failovers
npm i --save neo4j-ha-bolt-driver
const servers = [
['http://127.0.0.1:7474', 'bolt://127.0.0.1:7687'],
['http://127.0.0.1:7475', 'bolt://127.0.0.1:7688']
];
const auth = { user: 'neo4j', pass: 'password' };
const strategy = Neo4jHA.HAStrategies.roundRobin;
const rwConfig = Neo4jHA.HAReadWrite.masterReadWriteSlaveRead;
const checkInterval = 500;
console.log('connecting...');
const driver = new Neo4jHA(servers, { auth, strategy, rwConfig, checkInterval }, () => {
console.log('ready');
let session;
// we get a session and we tell the driver that we will perform at least one write
// the strategy is Round Robin but because we want to write
// and the read/write config is set to masterReadWriteSlaveRead (only master can write)
// this means that it will get a session to the master
session = driver.session(true);
// don't forget to close sessions when you're done with them
session.close();
// now we request a read-only session, so all servers are eligible for conenctions
// the first server in the list is chosen
session = driver.session(false);
session.close();
// requesting another read-only session will choose the second server
// because of the Round Robin
session = driver.session();
session.close();
// when you are done with the driver, just close it
driver.close();
})random: It will pick a random server from the poolroundRobin: It will round robin through the server poolnearest: It will pick the server with the lowest latency
masterOnly: Master will be the only queried servermasterWriteOnlySlaveReadOnly: Master is write-only and slaves are read-onlymasterReadWriteSlaveRead: Master is read-write and slaves are read-onlyall: All servers are read-write- Custom HAReadWrite Structure
{
master: { read: Boolean, write: Boolean },
slave: { read: Boolean, write: Boolean }
}error (-2): Server is returning an error in the HA checkunknown (-1): Server is in an unknown statedown ( 0): Server is unreachableup ( 1): Server is in the cluster
unknown ( 0): Server is in an unknown stateslave ( 1): Server is a slavemaster ( 2): server is master
const driver = new Neo4jHA(serverList, options, readyCallback);
Where:
serverList: is an array of[HTTP URL, BOLT URL]{bolt: Bolt URL, url: HTTP URL, auth: {user, pass}}
options:auth: {user, pass} - for all the connectionsstrategy: HAStrategiesrwConfig: HAReadWriteConfigneo4jDriverOptions: options passed on the the raw neo4j bolt drivercheckInterval: interval to check servers availabilityretryOnError: number of times to retry the query until the error is surfaced in callbackbadConnectionsCountAsErrors: true if a server connection error is counted as an error
readyCallback: callback when all servers status is known
const session = driver.session(writeLock[, rwConfig, strategy]);
Where:
writeLock: Boolean - Tell the driver ifif there will ve any writes in the sessionrwConfig: custom HAReadWriteConfig for that sessionstrategy: custom HAStrategies for that session
License can be found here