@nexfteam/single-instance
v0.0.8
Published
Check if an instance of the current application is running or not.
Maintainers
Readme
node-single-instance
This package intends to make it easier to have a single instance of an application running.
While the few other packages with the same purpose are either complicated (need a database) or doing the check only on Windows, single-instance try to be easy to use and for all platform.
Why all platforms ?
It is true that on OSX for example you doesn't need this check but you might want to do something in case someone try to run the application a second time (like focusing the running app for example).
Usage
- Install it:
npm install @nexfteam/single-instance- Use it !
import { SingleInstance } from "@nexfteam/single-instance";
const locker = new SingleInstance("my-app-name", {
// This option is optional
// You can customize the socket path
// socketPath: '/tmp/my-app-name.sock',
userSpecific: true,
// If you want not to use user specific socket path, set this to false(Default is true)
});
try {
await locker.lock();
// We just locked our application,
// now we can do what we want !
// You can also pass data to the locker:
// await locker.lock(JSON.stringify(process.argv));
} catch (err) {
// Quit the application
process.exit(0);
}
locker.on("connection-attempt", (data) => {
// When other instances try to connect, you can do something here,
// like focusing the running app for example.
console.log(data);
// If the connection has data, you can get it in the event data.
// If the connection has no data, data will be null.
});Support
- Bugs and feature requests: Github issue tracker
License
MIT
