vibe_ipc

Single-instance local IPC for Dart desktop apps, over a Unix domain socket.

Use it when a second process (a small launcher window, a CLI, a hotkey command, a second start of the same app) must hand a request to your running app, and start the app first if it is closed.

  • Local and private. A Unix socket in an owner-only folder (mode 700). No network port.
  • Validated. You list the allowed actions and check their arguments. Anything else is refused before your code runs.
  • Retry-safe. Every request has an id. Sending the same id again returns the first answer and does not run the action twice.
  • Ready ack. ping tells the sender whether your app has finished starting, so a request is never sent too early.
  • Start if closed. IpcLauncher starts your app once, keeps the request, waits for ready, then delivers it.
  • Safe on crashes. A stale socket file is replaced. A socket with a live owner is never taken over.

Install

dependencies:
  vibe_ipc: ^0.1.0

or dart pub add vibe_ipc.

Platform support

Linux and macOS (Unix domain sockets, dart:io). Windows and the web are not supported. Flutter desktop on Linux and macOS works.

Server (your app)

import 'package:vibe_ipc/vibe_ipc.dart';

final server = IpcServer(
  defaultSocketPath('myapp'),
  IpcSchema({
    'open': (a) => a['file'] is String ? {'file': a['file']} : null,
    'raise': IpcSchema.noArgs,
  }),
  (request) async {
    // request.action and request.args are already validated
    return IpcResponse(request.id, true, 'ok');
  },
  ready: () => appHasLoaded,
);
await server.start(); // throws IpcAlreadyRunning if another copy owns the socket

One handler per action (IpcRouter)

Instead of a switch inside one handler, register each action with its own validator and handler:

final router = IpcRouter()
  ..on('open', (r) async => IpcResponse(r.id, true, 'ok'),
      validate: (a) => a['file'] is String ? {'file': a['file']} : null)
  ..on('next', (r) async => IpcResponse(r.id, true, 'ok')); // no arguments
final server = IpcServer.router(path, router);

The router builds the schema for you, so an action cannot be accepted without a handler. Registering the same action twice throws.

Client (launcher, CLI, second start)

final launcher = IpcLauncher(
  IpcClient(defaultSocketPath('myapp')),
  () async => Process.start('/path/to/myapp', ['--background'], mode: ProcessStartMode.detached),
);
final result = await launcher.deliver(
  IpcRequest(launcher.newId(), 'open', {'file': '/tmp/a.txt'}),
  onPhase: print, // connecting, starting, waiting, sending, done
);
print('${result.ok} ${result.code}');

Result codes: the server's own code, or start_failed, start_timeout, ipc_timeout.

Notes

  • The socket path is $XDG_RUNTIME_DIR/<app>/ipc.sock, falling back to /tmp/<app>-<user>/ipc.sock. Set <APP>_IPC_SOCK to override it.
  • One request and one answer per connection. Messages are one JSON object per line, up to 200 KB.
  • The owner-only folder mode is set with the chmod program. If it is missing, the socket is still created and you should check the folder permissions yourself.
  • This protects against other users on the same machine. It does not defend against other processes running as the same user.

License

MIT.

Libraries

vibe_ipc
Single-instance local IPC over a Unix domain socket for Dart desktop apps.