Monk
A light Ruby web framework designed to be fully Ractor-safe: every app it produces is a valid Rack 3 app that is also Ractor.shareable?, so it can be served in parallel across Ractor worker pools without silently losing that safety property. Named after Thelonious Sphere Monk, great and unique Jazz piano player and composer.
Built in (loaded by require "monk"): routing, context and error handling, plain HTML forms (form params, flash messages, other sites' requests refused, security headers), boot-time Ractor-shareability checks, Monk::StateRactor for shared state, settings, ERB views, static assets and logging. Opt-in (each needs its own require): Postgres persistence and migrations, passwordless auth and sessions, email, background jobs on Postgres, a WebSocket server with Redis or Postgres fan-out, Monk::Live server-pushed HTML updates, presence, file storage, an installable app and web push. Details for each are in the Features table below.
Monk is Kino-agnostic — it's built on stdlib Ractor primitives only, with no runtime dependency on any particular server. Kino is the reference/development server (see bin/server), but any Ractor-aware Rack server, or a conventional one, can run a Monk app.
Requires Ruby 4.0+. Runtime dependencies: rack and base64.
Quick start
Install the gem — published as monkrb (the monk name on RubyGems belongs to an unrelated, long-abandoned project), the CLI and require stay monk:
gem install monkrb
monk new my_app && cd my_app
bundle install
bin/server # -> http://localhost:9292/hello
monk add --list # the modules you can add: postgres, auth, jobs, live...
monk new scaffolds the app's own Gemfile with gem "monkrb", "~> 0.26", require: "monk".
monk new writes a working skeleton (an HTML home page, a /hello route, a /api/hello JSON route, public/, its tests, a SETUP.md and an AGENTS.md for coding agents), with the app's own code under app/: app/app.rb and app/routes/ for routes, app/views/, and one directory per role (models/, presenters/, helpers/, mailers/, broadcasts/, jobs/). monk add adds Postgres, Redis, email, auth, background jobs, WebSocket and live updates, presence, file storage, an installable app, web push, and a Docker Compose deploy, to a new app or an old one, each with its wiring, commented examples of how to use it, and a test (monk new my_app --with auth,jobs does both at once): see docs/guides/scaffolding.md, which also says where each kind of code goes.
All monk commands and options are listed by:
monk --help
A taste
class App < Monk::Base
get("/hello") { "hello from monk" }
get("/users/:id") { json(id: params[:id]) }
get("/") { @title = "Home"; render "index", posts: Post.where(published: true) }
error(404) { json(error: "not found") }
end
run Monk.boot(App) # seals routes into a Ractor.shareable? structure, or raisesRoutes can't close over mutable state: Monk.boot raises Monk::UnshareableRouteError naming the offending route instead of failing on a live request. Shared mutable state goes through Monk::StateRactor.
Features
Everything beyond the core is opt-in (require "monk" alone loads none of it).
| Feature | What it is | Opt-in | Guide |
|---|---|---|---|
| Routing, context, errors |
get/post/…, path params, halt, json, error, experimental resources
|
— | routing.md |
| Forms | plain HTML forms: form bodies and nested params, _method, cross-site requests refused, csrf_field with auth, flash messages, redirects only to the app, security headers |
— | forms.md |
| Boot and shared state |
Monk.boot, Ractor-shareability checks, Monk::StateRactor
|
— | boot-and-shared-state.md |
| Settings |
Monk::Settings, MONK_ENV, env-var config validated at boot |
— | settings.md |
| Views and static assets | ERB compiled at boot, escaped by default, layouts/partials, frozen asset manifest | — | views.md |
| Logging | request log per environment, Monk::Log.debug/info/warn/error
|
— | logging.md |
| Persistence |
Monk::Persistence::Pg: raw pg, per-Ractor connections, hash-based Model
|
require "monk/persistence/pg" (+ .../pg/model); needs the pg gem |
persistence.md |
| Migrations | plain .sql up/down pairs, Migrator
|
require "monk/persistence/pg/migrator"; needs the pg gem |
migrations.md |
| Auth and sessions |
Monk::Auth: passwordless tokens, Bearer or cookie + CSRF, a per-address login-email rate limit, nightly cleanup with jobs |
require "monk/auth"; needs the pg gem and a registered Postgres connection |
auth.md |
Monk::Mail: text/HTML email over SMTP or a local relay, one MAIL_URL, sent from any worker Ractor, or from a background job with deliver_later
|
require "monk/mail"; deliver_later is there once monk/jobs is loaded too; SMTP needs the net-smtp gem |
mail.md |
|
| Background jobs |
Monk::Jobs: a queue in Postgres, run by bin/jobs on worker Ractors; retries with backoff, scheduled jobs, recurring jobs on a cron schedule (UTC), enqueue inside the app's own transaction, drain! for tests |
require "monk/jobs" (+ require "monk/jobs/runtime" in the job process); needs the pg gem and a registered Postgres connection |
jobs.md |
| WebSocket |
Monk::WebSocket: RFC 6455 server as its own process, Redis or Postgres fan-out |
require "monk/websocket"; fan-out: require "monk/websocket/redis_fanout" (+ the redis gem) or require "monk/websocket/pg_fanout" (+ the pg gem) |
websocket.md |
| Live updates |
Monk::Live: server-rendered HTML patches pushed to open tabs |
require "monk/live"; needs the WebSocket server and, across processes, Redis or Postgres |
live.md |
| Presence |
Monk::Presence: whether a signed-in person has a page open and visible (online), only hidden ones (away), or none (offline, with when they were last seen), across tabs, devices and WebSocket processes; an on_change hook in bin/server
|
require "monk/presence"; needs auth, the WebSocket server and Postgres; away needs Live |
presence.md |
| File storage |
Monk::Storage: files by key in any S3-compatible bucket (Hetzner, R2, B2, AWS), local files in development and test; browser uploads and downloads through signed links, never through a worker Ractor |
require "monk/storage"; no gem needed |
storage.md |
| Installable app |
Monk::Pwa: a web app manifest, icons and a service worker (/sw.js), checked at boot and linked by <%= monk_head %>, so the app installs on a Home Screen or as a desktop app |
require "monk/pwa"; no gem needed |
pwa.md |
| Web push |
Monk::Push: notifications to the devices where a signed-in person turned them on, encrypted and signed by Monk itself (RFC 8291, VAPID), one job per device; subscriptions tied to auth sessions |
require "monk/push"; needs auth, jobs and Postgres, and pwa's service worker |
push.md |
| Scaffolding |
monk new and monk add: one generator per module, for a new app or an existing one; text or --json output; monk add deploy for Docker Compose and Caddy on one host |
— (the monk command: monk new NAME --with ..., monk add MODULE, monk add --list) |
scaffolding.md |
More documentation
-
docs/framework-comparison.md: Monk vs. Sinatra vs. Rails, with LOC per module. -
docs/guides/deploying.md: worked deployment examples. -
docs/guides/deploy-hetzner.md: one app on a Hetzner Cloud server with Brevo for mail, every step, as tested. -
docs/design/ractor.md: how Monk uses Ruby'sRactor. -
docs/adr/andCONTEXT.md: architectural decisions and domain vocabulary. - Design docs and phase-by-phase plans behind each feature:
docs/design/views.md,docs/design/auth-sessions.md,docs/design/websocket.md,docs/design/persistence-ractor-connections.md,docs/design/live-pg-fanout.md, and the phase-by-phase plans and archived notes indocs/history/. -
CHANGELOG.md.
Working on this repo
bundle install
bundle exec rake test # Minitest
bin/server # demo app from config.ru via kino
More (server modes, Docker) in docs/development.md.
Status
Monk is pre-1.0 (see lib/monk/version.rb and the changelog): the API may still change between minor versions. The core is described in docs/history/core-plan.md. Features to add are listed in docs/roadmap.md. Done so far, each with its own design doc and plan: persistence, migrations, HTML templating and static assets, auth and sessions, WebSocket with Redis fan-out, log levels, live updates (Monk::Live, as of 2026-09-19), deployment support (Dockerfile scaffolding, docs/guides/deploying.md), the RubyGems release as monkrb (both 2026-09-22), and Monk::WebSocket::PgFanout/monk new --live --postgres — the same cross-process fan-out over Postgres LISTEN/NOTIFY instead of Redis, for an app that doesn't want a second piece of infrastructure (docs/design/live-pg-fanout.md, 2026-09-24), the built-in mailer Monk::Mail (0.17, 2026-09-26, ADR 0012), and background jobs on Postgres, Monk::Jobs, with mail sent from jobs (0.18, 2026-09-29, ADRs 0013 and 0014). In 0.19 (2026-10-01): monk new lays the app out under app/, one directory per role (ADR 0015); a WebSocket fanout listens only in the process that holds the sockets (#listen!); authenticate: :optional lets visitors' sockets in anonymously, for Live's rules to decide; and the WebSocket server stops cleanly on TERM (docs/design/websocket.md). In 0.20 (2026-10-05): Monk recovers from a dropped database connection (a Postgres restart, a failover) without a process restart. checkout reconnects a dead connection before running its block, never retrying the block itself; both fanouts' listeners reconnect and then make every open page resync; and a route that raises logs why (docs/history/plan-pg-reconnect.md). Also new: connection pools for Ractors that shouldn't each hold a connection (Monk::Persistence::Pg.pool). The WebSocket server's session checks, Live's subscribe rules and PgFanout's publisher can run through one, so open pages share a few connections instead of holding one each: 1,000 open sockets on 4 connections (docs/guides/persistence.md, "Pools"; docs/history/plan-pg-pool.md). In 0.21 (2026-10-09): scaffolding by module. monk new makes the core app, and monk add adds a module (Postgres, Redis, mail, auth, jobs, WebSocket, Live) to any app, new or existing, with its wiring, commented examples, a test, and its sections of SETUP.md and an AGENTS.md for coding agents; --json for agents and scripts. Redis and WebSocket become modules of their own, the WebSocket transport is chosen once per app, and monk new's module flags are gone (ADR 0017, docs/guides/scaffolding.md, docs/history/plan-scaffold.md). In 0.22 (2026-10-13): recurring jobs on a cron schedule, Monk::Jobs.recurring in config/recurring.rb, enqueued once per occurrence however many bin/jobs run (ADR 0016, docs/guides/jobs.md); auth housekeeping, with a login-email rate limit in the database, an index on sessions (subject) and a nightly cleanup job (docs/guides/auth.md); and an upgrade guide for apps made with an earlier Monk (docs/guides/upgrading.md). In 0.22.1 (2026-10-20): the WebSocket server queues a connect burst instead of collapsing under it, refuses a handshake it can't check with a 503, and takes more connects a second with thousands of sockets open: fewer allocations a connect, GC settings in bin/websocket_server, and WS_PROCESSES for several server processes on one port (docs/guides/websocket.md, docs/plan-ws-connect-ceiling.md). In 0.22.2 (2026-10-22): a fix for a handshake that could die when two connections were the first after boot (docs/plan-digest-preload.md). In 0.23 (2026-10-26): file storage, Monk::Storage, in any S3-compatible bucket (Hetzner, R2, B2, AWS) through Monk's own SigV4 signer, with local files in development and test that behave like a bucket. Browsers upload straight to the bucket and download through a redirect to a signed link, so no worker Ractor waits on the bytes, and monk add storage scaffolds the flow (ADR 0018, docs/guides/storage.md, docs/history/plan-storage.md). In 0.23.1 (2026-11-10): fewer allocations per request in models and views, about half as many objects for a page of 50 rows through a partial, so less GC on Ruby 4.0, where every GC stops every worker; responses are byte-identical. Route blocks also run as methods of Monk::Context instead of through instance_exec, the same on 4.0 and what lets Ruby 4.1's per-Ractor GC work. In 0.24 (2026-11-13): Live on real networks and presence. The WebSocket server closes a connection silent for two ping intervals, and the Live client pings while its page is visible, gives up on a silent socket after 10 s, and reconnects when the page is shown or back online, measured on iPhone and Android as well as desktop. Monk::Presence says who is online, away or offline, with an on_change hook to push it to pages, and monk add presence scaffolds it (ADR 0019, docs/guides/presence.md, docs/plan-live-networks-presence.md). In 0.25 (2026-11-18): an installable app, monk add pwa, and web push, Monk::Push, with monk add push: notifications to the devices where a signed-in person turned them on, encrypted and signed by Monk over OpenSSL, sent one job per device, with subscriptions tied to auth sessions (ADR 0020, docs/guides/pwa.md, docs/guides/push.md, docs/history/plan-pwa-push.md). In 0.26 (2026-11-20): plain HTML forms with no JavaScript: form bodies and nested params, _method, csrf_field, flash messages, and a route's own status. Monk is also safe by default, with no form helpers added: other sites' browser requests are refused on every route, every response carries security headers, and redirect goes only to the app. monk add deploy writes a Docker Compose stack for one host from the app's modules, with a tested Hetzner and Brevo guide. Presence also reads a closed iPhone app as away at once (docs/guides/forms.md, docs/guides/deploy-hetzner.md, docs/history/plan-forms.md).