Back to the blog
BunWebAssemblyGamesWebSocket

NFSU2 in the browser: what Bun has to do with it

November 09, 2026·6 min read·Diego Horvatti

Someone got Need for Speed Underground 2 running in a browser tab, with online mode. The project lives at nfsu2.xyz and went viral among devs for the usual reason. It's a 2004 game that needed a CD and a dedicated graphics card, and now it opens with a link. Seeing NFSU2 in the browser hits a lot of people right in the nostalgia. For me there's also a more interesting technical question. What has to exist on the server side for this to work without falling over?

A heads-up first: I won't claim to know the project's exact stack, because it isn't clearly documented. I'll use it as a hook and show how I would build the backend with Bun. That's where the topic touches the daily life of anyone writing TypeScript.

How does a 2004 game run in the browser?

In practice, there are three ways to bring an old game to the web:

  • Emulation or a compatibility layer compiled to WebAssembly. The original binary runs inside something pretending to be Windows XP with DirectX.
  • Porting the code, when the source code exists (official or reverse engineered) and gets recompiled with Emscripten to WASM + WebGL.
  • Reimplementing the engine, which reads the original game files and draws everything with new code.

Whichever path you take, the browser does the heavy lifting. WebAssembly runs the logic at near-native speed. WebGL or WebGPU draws the cars. The Gamepad API reads your controller. All of this has been mature for a few years. What changed is the nerve to do it with real games, several gigs in size, not just demos.

The bottleneck is rarely the client's CPU. It's the network. NFSU2 has hundreds of megs of textures, models and audio. If you send it all at once, the player gives up on the loading screen. I remember waiting for the CD progress bar back in 2004. Nobody has that patience in 2026.

Where Bun fits into this story

A project like this has two backend services, even if they're small:

  1. A server for large static assets, with aggressive caching.
  2. A real-time server for multiplayer: lobby, car positions, chat.

Bun handles both with its native API. No framework, no dependencies. To serve assets, Bun.file returns a lazy file. It's only read from disk when the response is sent:

Bun.serve({
  port: 3000,
  async fetch(req) {
    const path = new URL(req.url).pathname
    if (path.includes('..')) return new Response('nope', { status: 400 })

    const file = Bun.file(`./assets${path}`)
    if (!(await file.exists())) return new Response('404', { status: 404 })

    return new Response(file, {
      headers: { 'Cache-Control': 'public, max-age=31536000, immutable' },
    })
  },
})

That immutable is only safe if the file name has a hash (cars.a1b2c3.pak). The file changes, the name changes. That way the browser never downloads the same 80 MB pack twice. In real life I'd put a CDN in front, because serving gigs straight from your process means paying a lot for bandwidth.

Multiplayer with Bun's native WebSocket

The part that interests me most is online play. Bun has WebSocket built into Bun.serve, with native pub/sub. That's exactly the model of a racing lobby. Each room is a topic. Each car publishes its position. Everyone in the room receives it.

type Data = { room: string; player: string }

Bun.serve<Data>({
  port: 4000,
  fetch(req, server) {
    const url = new URL(req.url)
    const room = url.searchParams.get('room') ?? 'lobby'
    const player = crypto.randomUUID()
    if (server.upgrade(req, { data: { room, player } })) return
    return new Response('use WebSocket', { status: 426 })
  },
  websocket: {
    open(ws) {
      ws.subscribe(ws.data.room)
    },
    message(ws, msg) {
      // forward position/speed to the others in the room
      ws.publish(ws.data.room, msg)
    },
    close(ws) {
      ws.unsubscribe(ws.data.room)
    },
  },
})

That's about 25 lines. In Node you'd get to the same place with ws or Socket.IO, and it works fine. The difference is that in Bun it comes built in. And publish happens on the native side, so you don't loop over a list of clients in JavaScript for every message. With 8 cars in a room sending 20 updates per second, that's 160 messages per second coming in and over a thousand going out. Multiply by a hundred rooms and you see why this detail matters.

In an online game, the server doesn't need to be smart. It needs to be fast and trust no one.

What changes in your code when the client is a game

Here's the part that catches people coming from CRUD. A few lessons that apply to any real-time app, not just racing:

Send binary, not JSON. A position is x, y, z, rotation and speed. In JSON that's around 80 bytes of text. In a Float32Array it's 28 bytes. At 20 Hz per player, the difference shows up in your bandwidth bill and in lag.

// client: 7 floats = 28 bytes
const buf = new Float32Array([x, y, z, rx, ry, rz, speed])
socket.send(buf)

Validate on the server. The client is a browser. Anyone can open DevTools and send whatever they want. If the server just forwards messages, someone will teleport their car to the finish line on day one. The bare minimum is checking message size and rejecting impossible position jumps. It's tedious, but it's the same old rule: never trust input.

Rate limit. One buggy client sending 1000 messages per second takes down the whole room. A counter per connection, reset every second, covers 90% of cases.

Plan for reconnection from the start. Wi-Fi drops. A tab goes to the background and the browser throttles its timers. The player comes back and expects to be in the same race. That means storing room state somewhere other than just the connection's memory.

Is this even allowed? The legal part nobody wants to read

I'll be honest here. Need for Speed is an Electronic Arts trademark. The cars, the soundtrack (yes, that Snoop Dogg track with The Doors) and the graphics all have owners. Game preservation projects live in a gray area. History shows many of them end with a takedown request, no matter how well made they are.

That doesn't take away from the technical achievement. But if you're thinking about doing something similar, keep things separate. The engine, the netcode and the server are yours and can go in your portfolio. The assets of a commercial game are not, and distributing them publicly is asking for trouble. The safest way to get the same learning is to build your own little racing game, with free models, and use exactly the same architecture.

Is Bun worth it for a project like this?

My take: for this kind of backend, yes, no second thoughts. A file server plus WebSocket with pub/sub is the use case where Bun is most polished. You write TypeScript directly. No build, no ws, no Express, and the server starts in milliseconds. For a side project, that's half the motivation sorted.

Where I'd be careful: if the game grows to thousands of rooms, the bottleneck stops being the runtime. It becomes shared state across multiple instances, because Bun's publish only reaches clients connected to the same process. That's where Redis, NATS or something similar comes in to distribute messages. No runtime solves that for you.

NFSU2 running in a tab reminded me of something. The web has become a serious gaming platform, and the backend behind it fits in a single TypeScript file. I really like this combo of a simple tool and a hard problem. If you want to see what I've been building along these lines, check out my projects.

LinkedIn summary

Someone got Need for Speed Underground 2 running in a browser tab. With online mode.

A 2004 game that needed a CD and a dedicated graphics card now opens with a link. WebAssembly and WebGL already handle the heavy lifting.

For me, the more interesting question is what keeps the multiplayer server standing.

With Bun, about 25 lines of TypeScript handle the lobby: native WebSocket, one room per topic and publish running on the native side.

The rest is the usual discipline: send binary instead of JSON, validate everything on the server, rate limit and plan for reconnection from day one.

I wrote on the blog about how I would build this backend and where it starts to strain. If you like simple tools solving hard problems, give it a read.

#Bun #TypeScript #WebSocket #WebAssembly #GameDev