Internals

Virtual server

How an HTTP server running inside the VM is reached from a real browser iframe — without a real network.

The problem

A Node server inside NanoVM binds to a socket inside the emulator. There is no real TCP stack and no real port for the browser to connect to.

Request injection

The demo registers a Service Worker that intercepts requests to the preview origin. Each request is serialized and injected into the VM’s socket layer as if it had arrived over the network; the in-VM server handles it and produces a response, which is passed back out to the Service Worker and returned to the iframe.

  • Browser iframe issues a request to the preview origin.
  • Service Worker intercepts it and forwards it to the host bridge.
  • The host injects it into the VM’s listening socket.
  • The in-VM server responds; the response is streamed back to the iframe.
Because the whole round-trip is synchronous from the iframe’s perspective, live preview of an in-VM server feels exactly like hitting localhost.

Reference

The full request path is documented in docs/virtual-server.md.