title: How to prevent multiple instances in Electron
body: <p>I do not know if this is possible but I might as well give it a chance and ask.
  I'm doing an Electron app and I'd like to know if it is possible to have no more than a single instance at a time.</p>
  
  <p>I have found this <a href="https://gist.github.com/olegam/10b87e3e54a859506a8d" rel="noreferrer">gist</a> but I'm not sure hot to use it. Can someone shed some light of share a better idea ?</p>
  
  <pre><code>var preventMultipleInstances = function(window) {
      var socket = (process.platform === 'win32') ? '\\\\.\\pipe\\myapp-sock' : path.join(os.tmpdir(), 'myapp.sock');
      net.connect({path: socket}, function () {
          var errorMessage = 'Another instance of ' + pjson.productName + ' is already running. Only one instance of the app can be open at a time.'
          dialog.showMessageBox(window, {'type': 'error', message: errorMessage, buttons: ['OK']}, function() {
              window.destroy()
          })
      }).on('error', function (err) {
          if (process.platform !== 'win32') {
              // try to unlink older socket if it exists, if it doesn't,
              // ignore ENOENT errors
              try {
                  fs.unlinkSync(socket);
              } catch (e) {
                  if (e.code !== 'ENOENT') {
                      throw e;
                  }
              }
          }
          net.createServer(function (connection) {}).listen(socket);;
      });
  }
  </code></pre>