Async changes to main.clj to combat lag?
I'm a little out of my depth here but wonder if there are a few points in the code that are inadvertently blocking, contributing to #89. Like here: https://github.com/mtgred/netrunner/blob/master/src/clj/game/main.clj#L50
If I'm reading the code right, this while loop needs to execute for each socket connection, which could be how slow internet connections lag the site for everyone. Is this the kind of thing that should be wrapped in a go? And/or should there be a (chan) here?
Just dipping my toes in because I'm surprised that the app insn't able to handle many concurrent connections, even on a crappy server - that's supposed to be one of clojure's strengths.
Disclaimer: I know enough clojure to be mildly dangerous but don't have any code in production.
It's actually an internal ZeroMQ socket connection between the Node server and Clojure game server. The game server waits for a message from node, process it and return a new game state to Node. I think that code is fine although i'm not an expert in ZMQ at all and there could be some optimisation to be done there.
I think the lag comes from sending too much data to the clients. The whole game state is currently send to both players after each action. A full game state is around 30-200kb of json. During peak time, the server sends data at a rate of 20-40 mbps. I think sending only a diff would improve the lag significantly. But then additional logic needs to be added to send a full game state when a client want to reconnect to a running game.
Ahh got it- thanks for the explanation. I'll read up on ZeroMQ because I have 0 experience with it.
Maybe gzipping the json between client and server could be a drop-in solution to decrease bandwidth usage? I reckon 2-4x drop in size if it's sent raw right now(?)
Diffs would be the optimal solution but I don't know if it's as drop-in of an addition. You've prolly already looked into it but looks like fitbit has open sourced something: https://github.com/flitbit/diff
Netrunner game, exercise data, close enough :)
My attempt at helping here... looking for simple things to reduce network load.
First thing I noticed when looking at WebSockets traffic: every time a new Game is created (or updated, I imagine), a games array is pushed to each client over the websocket. That array is currently (18:03 UTC, 13 June) ~340k of data, because it appears to be pushing each player's deck list for all games in which decks have been selected (including active ones). I randomly picked a deck which weighs in at 11kb of JSON. That seems like a lot of excessive data; can the decklist push be done when joining the game instead? This also shows that "ghost" games might indeed be hurting server performance... if those games have a deck selected, then every game update is passing the deck JSON to every single client...
Otherwise, the WebSockets standard does support gzip-like compression on socket frames called "permessage-deflate." It looks like Socket.io won't support that standard until 1.4 (see here), but it would perform gzip-like compression on the socket stream instead of sending the full JSON raw.
Thanks for the investigation. The lobby code has been updated to remove the deck lists when a game is started. Ghost game are now cleared after 1 hour.
With 19 real games active, the games array today (16:39 UTC, 14 June) is 22kB, and the lag seems a lot better.
Awesome! Thanks to you both on this. I'm closing this issue.
The change has a significant impact on the network traffic and it can be felt in actual usage.

Awesome! Lower server costs + better user experience = :beers:
@nealterrell FTW
Group effort! @eah13 turned me on to investigating compression and diff communication, which meant I had to look at HTTP headers coming from the server, which led me to the large websocket traffic... and then obviously @mtgred had to go and fix the thing, which had to be harder than looking at some traffic printouts in Chrome :D.