This is a Rails Application that uses WebSockets and the react-crossword component to create multiplayer crosswords. You can read a blog post about why I built it and how it works.
You can see a demo at multicrosser.chriszetter.com.
To run this project:
./bin/setup
to install dependencies./bin/rails crosswords:load_from_feed
to load the latest crosswords to display on the homepage./bin/rails server
to start the projectHere's what happens when a player types a character:
react-crossword
calls setCellValue
to update the grid
setCellValue
calls the onMove
callback with cell location and valueonMove
callback calls the move
function in the action cable subscriptionmove
function sends the move to the serverMovesChannel#move
is run
received
function runs in the Action Cable subscriptions which calls the onReceiveMove
callbackonReceiveMove
calls setCellValue
with the triggerOnMoveCallback
option set to false
so onMove
isn't called againsetCellValue
updates the crossword girdIf the move can't be broadcast with Action Cable it's stored in the MoveBuffer
. On reconnection:
When the move MoveBuffer
is replayed, moves will only apply if the cell they change still has the same character in it when the move was made. For example, if you change an 'A' to a 'B' while offline this move will be discarded if someone has since changed the 'A' to a 'C' and broadcast it to the server before you.
The MoveBuffer
uses local storage so will persist if the page is refreshed or the browser is closed.
Crosswords are scraped from the Guardian Crossword pages which contain a JSON representation of each crossword. The crosswords are re-used following their Open Licence Terms.