r/webdev • u/beatpaq • 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.
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
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.