This is simple multiplayer game 'reversi'. Using this game I want to demonstrate the way of communication with client using socket.io. Idea is to use regular routes and controllers instead of implementing socket.io stuff manually.
If you like to use socket.io with your railwayjs app, all you need is to add
line to your 'npmfile.js' (and of course install it
npm install rw.io socket.io)
That's it - now you can use 'socket()' method inside your controllers. And 'socket' method in routing map.
Install with firstname.lastname@example.org
- clone repo, run
- remove railwayjs:
rm -rf node_modules/railway
- get railwayjs from github, switch to branch 1.1.0
- install railway:
npm install path-to-railway(inside app dir)
open http://localhost:3000/ in two different browsers (we need different sessions)
Goal of this demo app: show example of usage socket.io in railway, so, first of
all I want you to check controller:
It has two actions,
thegame for http get request,
move for socket request.
Both actions configured in routes
Idea is following: we have single method exposed to controller, called
it used for interaction with client. When
socket() called without params it
returns current user's pipe, so you can send messages directly to owner of
session (if he has multiple tabs with same sessionID all of them will receive
If you need to communicate with another user you need to know his SessionID. It
If you need to send broadcast message... ask me, and I will expose another API, or, better fork tiny lib rw.io and add this API, it's pretty easy.
Skip client-side part
Client-side part may look difficult, this is because reversi game was written very poorly more than two years ago, and i'm too lazy to improve it.
Basically it just standard socket.io stuff:
- subscribe to particular events:
- communicate with server part: