Skip to content
Eugene Lazutkin edited this page Jul 1, 2026 · 4 revisions

Events

double-meh ships a tiny synchronous event emitter bolted onto the io instance. It is general-purpose: you register string-keyed listeners with io.on, remove them with io.off, and dispatch with io.emit. All three are chainable and return io.

import io from 'double-meh';

const onReady = () => console.log('io is ready');

io.on('ready', onReady);
io.emit('greet', 'world'); // runs any 'greet' listeners
io.off('ready', onReady);

io.on(event, fn)

Subscribe fn to event. The same function may be registered more than once (it will be called once per registration). Returns io, so calls chain.

io
  .on('ready', () => console.log('ready'))
  .on('error', err => console.error(err));

io.off(event, fn)

Unsubscribe. Removes the first registration matching fn for event by identity — so keep a reference to the exact function you passed to on. Unknown events and unknown functions are silently ignored. Returns io.

const handler = payload => console.log(payload);
io.on('update', handler);
io.off('update', handler); // must be the same reference

io.emit(event, ...args)

Dispatch event synchronously, forwarding every extra argument to each listener in registration order. Listeners run over a snapshot of the list, so a handler may safely on/off during dispatch without affecting the current pass. Returns io.

io.on('progress', (loaded, total) => {
  console.log(`${loaded}/${total}`);
});
io.emit('progress', 512, 1024); // logs "512/1024"

The 'ready' event

The only event the library emits on its own is 'ready'. It fires once during code-forwarding startup (installCodeForward), after any server-injected __doubleMeh prelude — queued setup callbacks, in-flight requests, and pre-arrived responses — has been drained. Use it to run code that depends on that handoff being complete.

io.on('ready', () => {
  // prelude drained; safe to issue follow-up requests
  io.get('/api/session');
});

Custom events

Beyond 'ready', the emitter is entirely yours: pick any event name and use io as a lightweight app-wide bus. double-meh never emits custom names itself, so there is no risk of collision.

io.on('auth:expired', () => io.get('/api/refresh'));

// elsewhere, e.g. from a response inspector
io.emit('auth:expired');

See also

Clone this wiki locally