
HN Plays 2048 - computer
https://hnplays2048.herokuapp.com/
======
Oculus
Took a look at your code and noticed you're using Socket.io. Don't use
Socket.io for anything over double digit concurrent users. It's well known to
be horrible at scaling. If you want something that's _better_ at scaling I'd
suggest SockJS[1].

1 - [https://github.com/sockjs/sockjs-node](https://github.com/sockjs/sockjs-
node)

~~~
babuskov
The "over double digit" claim is simply not true. I'm running three
multiplayer games for about two years now, and it starts to have problems once
if goes over 3500 concurrent connections. It could be that I just hit the
right combination of node+socketio versions, though, as I had problems with
newer ones. My setup is:

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.

~~~
feronull
We also do had a memory leak because of socket.io 0.9.6

take a look at this discussion
[https://github.com/LearnBoost/socket.io/issues/1303#issuecom...](https://github.com/LearnBoost/socket.io/issues/1303#issuecomment-33107520)

~~~
babuskov
Interesting, but I'm not using Redis nor MemoryStore. I gave each machine
behind HAProxy a different hostname (mapped to the same IP address), and I
route the clients in roundrobin fashion to each of those before they open the
page with socket.io javascript. It works like this:

\- 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.

~~~
thedufer
How does this work for non-socket transports? You have to be saving the
broadcasts in case someone is polling, right?

~~~
babuskov
I'm not sure what you mean by "saving the broadcasts". Can you give an
example?

Some 11% of clients are using XHR or JSONP protocols and it it Just Works(tm),
I did not add anything special.

~~~
thedufer
You said you don't use Redis/MemoryStore, but when you broadcast something it
has to be saved somewhere until all of the polling sessions get it. Where is
it being saved?

------
granttimmerman
Hey guys, it's the creator here.

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!

[https://github.com/grant/hnplays2048](https://github.com/grant/hnplays2048)

~~~
addisonj
I literally was going to do this this coming weekend, same name even!

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...](https://www.rabbitmq.com/blog/2012/05/14/introducing-rabbitmq-web-
stomp/)

------
grej
Kind of fun, but I agree with some of the other comments that it would be
interesting to see what would happen if the app made a move every 15-20
seconds based on the vote tally in that time window.

~~~
cube_yellow
Right. It needs a Democracy mode. maybe even a Democracy/Anarchy slider
controlled by Q and E.

~~~
dbieber
Ask and you shall receive
[http://hnplays2048-democracy.herokuapp.com/](http://hnplays2048-democracy.herokuapp.com/)

Edit: Now integrated into the OP's app

~~~
zeyus
Users should only be able to vote once (maybe be allowed to change it, but
right now someone can overrule the consensus by hitting an arrow key as many
times as they can)

Nice work though!

~~~
gahahaha
Isn't that a metaphor for democracy in America today?

------
colept
This game is no more playable than it was when it was posted hours ago. And
here's why:

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.

~~~
granttimmerman
Good idea. Working on it.

------
begriffs
It's currently unplayable. Instead imagine if it would make one move every
half a minute. During the time between moves anyone could vote on the next
move. Then the winning move would be played. It would be a slow game, but
would be the aggregate HN wisdom.

~~~
sillysaurus3
This is awesome. It's anarchic and chaotic and amazing.

I can't wait until it falls off the frontpage and drops to 5 users.

EDIT: We killed it. Nooo.

~~~
granttimmerman
Should be online now.

------
dbieber
I added democracy mode...

The pull request is here:
[https://github.com/grant/hnplays2048/pull/10](https://github.com/grant/hnplays2048/pull/10)

You can play it here:
[http://hnplays2048-democracy.herokuapp.com/](http://hnplays2048-democracy.herokuapp.com/)

~~~
richard_cubano
I like this one a bit better because it shows vote totals and has a visible
countdown:

[http://voting-2048.herokuapp.com/](http://voting-2048.herokuapp.com/)

~~~
richard_cubano
Pull Request if anyone wants to see the code. Sorry this is in 2 places.

[https://github.com/grant/hnplays2048/pull/13](https://github.com/grant/hnplays2048/pull/13)

------
bhaumik
Is this HN's version of twitch plays Pokemon?
[http://www.twitch.tv/twitchplayspokemon](http://www.twitch.tv/twitchplayspokemon)

~~~
saraid216
Yes. Explicitly.

[https://news.ycombinator.com/item?id=7431436](https://news.ycombinator.com/item?id=7431436)

------
lerg
I've made a version of 2048 on Hexagonal Board - check it out
[https://play.google.com/store/apps/details?id=com.spiralcode...](https://play.google.com/store/apps/details?id=com.spiralcodestudio.hexled)

------
spajus
This makes a perfect illustration why startups are shit. Instead of working on
something useful, people either produce crappy javascript games, or play them.
Now massively downvote this comment, write 100 replies explaining why I'm
wrong and how retarded I am for trying to say something like this, then go
read something by Paul Graham and play more clones of 1024. And then write a
long essay why your startup failed.

------
Smirnoff
Damn, this version seems so unplayable-- so much chaos. When I play on my own,
I already do so many random moves until I reach 512. After that every move
that I make is evaluated. I have no problems reaching 2048 (or 29,000 points)
within 10-15 minutes.

Here, I just get frustrated with the slowness of the moves (not to mention the
strategy of the "democratic group" is way off)

------
vinh0811
Here is my app for Android:
[https://play.google.com/store/apps/details?id=com.contraryki...](https://play.google.com/store/apps/details?id=com.contrarykid.twos)

~~~
izzydata
Nice. I shall try this and never be productive for the rest of my life with
all these 2048 forks.

------
munimkazia
Utter chaos going on in the game over there. But it's a fun experiment.

------
ghiculescu
This is probably a bit much, but I'd love to see someone whip up a multiplayer
2048 using something like Firebase. It would probably start many an argument.

------
martinthenext
It's pretty interesting how the strategy derived by averaging over multiple
intelligent users can be that sub-optimal.

------
joeblau
Can someone index all of these versions :)

------
comrh
This seems to show what I thought when I originally saw the game, that random
spamming would get you pretty far.

------
coreymgilmore
It would be nice to know your own user #.

~~~
granttimmerman
Good idea! I'll make an issue for this on Github.

~~~
granttimmerman
Your own user inputs are now bolded

Commit:
[https://github.com/grant/hnplays2048/commit/308f68341b2c54bf...](https://github.com/grant/hnplays2048/commit/308f68341b2c54bf672b40c0c6de300bbd72af10)

------
Dobbs
This needs a delay. That way you can follow the chaos instead of just staring
at a moving screen.

------
yaddayadda
[http://xkcd.com/1344/](http://xkcd.com/1344/)

------
tomaskazemekas
It is a good demonstration that in cases like this user democracy is a loosing
strategy.

------
granttimmerman
Man... scalability sucks.

------
gk1
A-a-and it's down.

------
mcescalante
Another case of "HN crashes the heroku app"

~~~
evidencepi
LOL

------
Cub3
We really need a democracy mode for this

~~~
fblp
it has been added and is awesome

------
azinman2
I really don't like to hate on other people's work but seriously more 2048
forks? And now ones that aren't even remotely playable?

~~~
theandrewbailey
Make 2048 forks, not war.

------
kregasaurusrex
Start9

~~~
stefan_kendall3
Anarchy lasted minutes before the democracy babies came running in.

------
theandrewbailey
seems like it falls victim to popularity, and crashes often.

------
maaarghk
holy hell this makes me hate you all! =P

------
joyeuse6701
the chaos.

------
evidencepi
Crashed!!

------
mkoryak
you guys really suck at this game.

~~~
dwd
I'd love to give people the benefit of the doubt and attribute it to spoilers
who intentionally move it against the flow, but I have my doubts.

------
ankit84
A joKE?

