Skip to content

Dev channel

The dev channel is how a module and an open page talk to each other under hamur dev. A module sends a message to every page when something it watches changes, and a page can send one back. It is the path for what Vite plugins do with server.ws.send and import.meta.hot.send; Bun 1.4.4 implements neither import.meta.hot.send() nor custom server events, and import.meta.hot stays Bun's own object (HMR in app code).

The channel rides on the socket that already reloads pages, /_hamur/reload, so it needs no port, and the socket's origin check covers it. Every message is one JSON object.

From a module to the page

useDevChannel() of hamur/kit works in setup and in any hook. send(event, data) reaches every page that loaded the dev client; on(event, handler) receives what a page sends and gets a client that answers that page only. Both are hamur dev only: in a build and in hamur typegen the channel is inert, send does nothing and on never calls, so a module needs no branch.

ts
import { defineModule, useDevChannel } from "hamur/kit"

declare module "hamur" {
  interface HamurDevEvents {
    "notes:update": { file: string }
    "content:ping": { at: number }
    "content:pong": { at: number }
  }
}

export default defineModule({
  meta: { name: "content" },
  setup() {
    const channel = useDevChannel()
    channel.on("content:ping", (data, client) => client.send("content:pong", { at: data.at }))
  },
  hooks: {
    "dev:change": (change) => {
      if (change.relative.startsWith("content/")) useDevChannel().send("notes:update", { file: change.relative })
    },
  },
})

The dev:change hook is the usual trigger. hamur dev calls it once for every file event it watches, after the ignore rules (node_modules, .hamur, .output, .git, temporary files) and before its own handling of the batch. The hook gets a DevChange: type, file, relative, and two flags it may set, reload (reload every page once the server is rebuilt) and rebuildServer (start a server build). A hook that throws logs [hamur] dev:change hook failed for content/a.md: <message> and the batch goes on. See DevChange.

From the page to the module

onDevEvent(event, handler) and sendDevEvent(event, data) of hamur/app are the page side. Call them from a client app plugin, a component or any client code.

ts
import { definePlugin, onDevEvent, sendDevEvent } from "hamur/app"

export default definePlugin(() => {
  onDevEvent("notes:update", ({ file }) => {
    document.body.dataset.content = file
  })
  onDevEvent("content:pong", ({ at }) => {
    document.body.dataset.rtt = String(Date.now() - at)
  })
  document.addEventListener("click", () => sendDevEvent("content:ping", { at: Date.now() }))
})
  • Both do nothing on the server and in a production build, which drops them: the production client holds no __hamurDevChannel.
  • A message sent before the page's socket has opened is lost. Send in answer to something the user or the page did, not while the app starts.
  • A throw in a page handler goes to reportError; a throw or rejection in a module's on handler is logged [hamur] dev event handler for content:ping failed: <message>.

Names, data and types

An event name is <module>:<event> in lower case, such as content:update. A name without the module part is hamur/kit: dev event "update" must be named "<module>:<event>" from a module and a TypeError of the same text from a page; hamur:, bun: and vite: are reserved (hamur/kit: dev event "hamur:reload" is reserved). data must survive JSON.stringify, so a function, a symbol or undefined fails with hamur/kit: useDevChannel().send("content:update"): data is not JSON (<reason>). A page message over 1 MiB is dropped and logs [hamur] a dev event over 1 MiB from a page was dropped (content:update); a page message with an invalid or reserved name is dropped without a log.

A module types its events by adding to HamurDevEvents of hamur, as the sample does. An event with no entry takes unknown.

What is tested

  • Names, the JSON check and the size limit: packages/hamur/src/shared/dev-channel.test.ts, packages/hamur/src/kit/dev-channel.test.ts, packages/hamur/src/app/dev-channel.test.ts.
  • The socket protocol and the runner: packages/hamur/src/dev/dev-channel.test.ts.
  • A content:update sent from dev:change changes the page text without a reload, and sendDevEvent reaches on and the client answers: tests/browser/dev-channel.test.ts (browser runner).
  • dev:change calling updateTemplates, rebuildServer and a throwing hook: tests/dev-change.test.ts.
  • No __hamurDevChannel in the six production clients even when a plugin calls both functions: tests/prod-markers.test.ts.

The API is in hamur/kit and hamur/app.