HN Plays 2048
hnplays2048.herokuapp.com
hnplays2048.herokuapp.com
node.js 0.6 socket.io 0.9.6
This version of socket.io has a nasty bug in jsonp protocol handling which I patched manually by merging some change from a newer version, but it does work fine.
To make it scale, I run multiple node processes. In the last 300+ days each of those has between 1000 and 2000 concurrent connections all the time and it works fine. Except that there is some memory leak (I'm not sure if it's related to socket.io or some other lib), so I restart each server once a week (it's done automatically each Sunday at 2am).
I had my eye on SockJS since 2012, and it looks like newer versions are really good, but I'd need upgrade to newer nodejs and this is simply not possible for the existing projects as many used modules are not available.
However, if you're starting a new project, it's worth testing and see which performs better.
take a look at this discussion https://github.com/LearnBoost/socket.io/issues/1303#issuecom...
- client opens a page on HAProxy load-balancer picked server
- the actual node server that receives the request, redirects the browser to its custom hostname using the same URI
- client opens the same page on the particular server and socket.io connects to that subdomain
This ensures that all future requests (if socket.io connection is broken) will go to the same server without Redis, MemoryStore, cookies or whatever.
Drawbacks:
- one additional HTTP redirect when page that contains socket.io is loaded (luckly, in games there aren't many of those)
- if you want to retire some sub-domain hostname, you have to be careful to allow existing clients to re-connect somewhere else. Not really a problem for my setup, but YMMV.
Some 11% of clients are using XHR or JSONP protocols and it it Just Works(tm), I did not add anything special.
You're the first person I've heard that was able to run vanilla Socket.io at such scale. I know Trello and some other shops use Socket.io, but they've forked & changed it to something that looks nothing like the original[2].
How this is helping?
"Flash is absolutely not required for Socket.IO to function. If Flash is available, it'll be leveraged, as it provides almost the same capabilities as WebSocket. If it's not, the next best transport will be chosen." - Socket.io
I never thought this little project would get this big!
If someone wants to help me scale this, or add things like chat, please do! Just send a PR on Github!
I was going to take a much different approach however - I was going to use rabbitmq speaking STOMP over a websocket [0] with two queues, one for client moves, which the server would consume and write to the moves queue, which each client would consume. This would allow the server to do more fancy rate limiting / throttling as not to just lead to a message storm. This, along with storing the game state in a db, allows for scaling out app servers.
0 - https://www.rabbitmq.com/blog/2012/05/14/introducing-rabbitm...
Edit: Now integrated into the OP's app
Nice work though!
Edit: pull request here if anyone wants to see the code:
tl;dr: anarchy produces all the fun crazy ridiculous happenings, it's what makes TPP fun. If it's "taking too long" and you want to beat the game quickly just play solo; beating the game fast isn't the point.
setInterval(function() { window.manager.inputManager.emit("move", Math.floor(Math.random() * 3) + 1); }, 1000);
Some rate limiting has been put in place, but it's not good enough. If there are 50 users online at once, they all shouldn't be able to spam a command every second.
A better algorithm would be:
seconds_between_commands = 60 - (60 / number_of_players);
With a clock letting you know when you can make your next move.
Otherwise, it's just way too easy to spam.
I can't wait until it falls off the frontpage and drops to 5 users.
EDIT: We killed it. Nooo.
The pull request is here: https://github.com/grant/hnplays2048/pull/10
You can play it here: http://hnplays2048-democracy.herokuapp.com/
Here, I just get frustrated with the slowness of the moves (not to mention the strategy of the "democratic group" is way off)
Yuck. How much for that 2048 tile again?
It's completing the puzzle in less than 5 seconds at the rate that its spamming commands.
:)
Commit: https://github.com/grant/hnplays2048/commit/308f68341b2c54bf...