Skip to main content

Online Card Game with Node.js and Socket.io – Episode 7

2 min read

Older Article

This article was published 13 years ago. Some information may be outdated or no longer applicable.

For those of you who’ve been following along, this is the one you’ve been waiting for. After six previous posts about an online card game, I’m finally releasing the code.

Take the codebase with a pinch of salt. It’s not the prettiest JavaScript you’ll ever read, and I know there are things in there that don’t work as expected. But I got a lot of emails from you asking to see it, so here it is.

Let’s walk through the basics. Once you’ve checked out the repository from GitHub, follow these steps to get it running:

  1. npm install && bower install will install the appropriate node packages such as socket.io and express as well as the required ‘frontend’ CSS and JS libraries (jQuery and Bootstrap)
  2. Edit server.js and make sure that the port is suitable for your needs (by default the application launches on port 8080).
  3. Edit client.js and make sure that you connect to the IP/port that you’ve set up in the previous step
  4. Execute node server.js

That should be it. Navigate to the IP/port combination you specified, and you’ll see a window asking for a Player’s name. Enter it, then wait for a second player to join the “battle”.

A few things worth flagging:

  • The room ID is hardcoded to 1. That needs to change, and I’ll get to it when time allows.
  • There’s literally no handling for when a client disconnects (on purpose or by accident). Not ideal.
  • Rooms and tables can’t be created yet. That’s coming.

The card images are pulled from Wikipedia. I’m still waiting on the right icons from Font Awesome. Once those land, I’ll be able to generate my own cards with CSS.

Constructive feedback is welcome.