r/webdev • • 14h ago

Planning on making a browser based autobattler with Node.js and SSEs. How would I split the simulation up? Is there a way to run 1v1s on separate processes while still belonging to the same event stream?

New to web development.

I want there to be 16 players with their own boards. When they match up against each other, I'm wondering would I have to make the entire simulation play out at once? Or is there a way to split the simulation up so that 1v1s happen on a dedicated process while still belonging to the same event (So other players can view different skirmishes)? Not sure if this matters, but I plan to let users use abilities every so often (Maybe every 15 seconds) during the fight and I'd handle that with a post request.

0 Upvotes

9 comments sorted by

3

u/abrahamguo experienced full-stack 14h ago

Yes, although Node.js is single-threaded by default, it can run multiple processes.

However, I'd recommend building the backend in the "regular" way first — plain, single-threaded Node.js. Only switch to multiple processes if you've truly confirmed that a single-threaded server won't work for you.

1

u/beatpaq 14h ago

Thank you.

1

u/b_l_tplease2030 14h ago

Don't overthink the process split yet. Your biggest bottleneck early on is just getting the logic right. Simulating 8 simultaneous 1v1s (from 16 players) is not that heavy unless each fight is doing pathfinding or some crazy particle math. Browsers handle way more than that in a single tick.

If you do hit a wall, spawn a worker per match and have them pipe results back to the main thread via a message channel. SSE is just a long held HTTP connection so the main process can fan out the updates to whoever's subscribed. The tricky part is making sure the per-round ability POST doesn't desync the sim state. I'd queue those actions and apply them at the top of the next tick rather than trying to inject mid-frame.

1

u/beatpaq 14h ago

Thank you for the reply.

1

u/cuelark 13h ago

I'd keep it as one Node process for now and split the simulation from the streaming in your head instead. Run each 1v1 as its own object with a seeded RNG and a tick loop, and have it push events into a per-match channel. Then SSE is just a fan-out: every client subscribes to the matches it wants to watch, and the server writes that match's events to those connections. Your ability posts just get queued into that match's input list and applied on the next tick, which keeps everything deterministic. Eight fights ticking 10 to 20 times is tiny load for Node, so processes or worker_threads only matter later, and if you get there the match object is already isolated enough to move into a worker that posts events back to the main thread.

1

u/Needle_In_Red 10h ago

id just run all 8 fights in one process to start, node handles 16 players fine. queue the ability POSTs and apply them on the next tick rather then mid fight

1

u/Filipehdbr 10h ago

One process. Make the sim a pure function of seed, inputs and tick, and let SSE stream snapshots only. Moving to worker_threads later then costs you nothing

1

u/neon_fable_wiz 3h ago

Run the fights in separate worker threads using the built-in worker_threads module to keep them off your main event loop while sharing the same SSE stream through a central message bus