Module: daemon-client
IPC client for communicating with the habit-tracker daemon.
This module provides functions to interact with the background daemon that manages website blocking via /etc/hosts modification. Communication happens over a Unix domain socket at ~/.habit-tracker/daemon.sock.
Note: This module is Node.js-only and should be imported directly:
import { notifyDaemon, pingDaemon, resetHosts } from '@habit-tracker/shared/daemon-client';Functions
notifyDaemon
▸ notifyDaemon(): Promise<boolean>
Notifies the daemon to refresh its blocking state.
Call this after any change that affects blocking (habit completion, deadline change, settings update). The daemon will re-evaluate which habits are overdue and update /etc/hosts accordingly.
Returns
Promise<boolean>
Promise resolving to true if daemon acknowledged, false if daemon is unavailable or didn't respond in time
Example
// After completing a habit
await completeHabit(habitId);
const notified = await notifyDaemon();
if (!notified) {
console.warn('Daemon not running - blocking state may be stale');
}Defined in
pingDaemon
▸ pingDaemon(): Promise<boolean>
Checks if the daemon is running and responsive.
Sends a ping command and waits for a pong response. Use this for health checks or to verify daemon connectivity before operations.
Returns
Promise<boolean>
Promise resolving to true if daemon responds with "pong", false if daemon is unavailable or unresponsive
Example
const isRunning = await pingDaemon();
if (isRunning) {
console.log('Daemon is healthy');
} else {
console.error('Daemon is not responding');
}Defined in
resetHosts
▸ resetHosts(): Promise<boolean>
Emergency reset: removes all habit-tracker entries from /etc/hosts.
Use this for emergency unblocking when the user needs immediate access to blocked websites. The daemon will remove all entries between the # HABIT-TRACKER-START and # HABIT-TRACKER-END markers.
Warning: This bypasses the normal blocking logic. Websites will remain unblocked until the daemon's next check cycle or until notifyDaemon() is called.
Returns
Promise<boolean>
Promise resolving to true if reset succeeded, false if daemon is unavailable or reset failed
Example
// Emergency unblock button handler
async function handleEmergencyReset() {
const success = await resetHosts();
if (success) {
alert('All websites unblocked');
} else {
alert('Failed to reset - is the daemon running?');
}
}