Is telling a client to do minor html updates appropriate use of socket.io?

Novice at socket.io

Writing a simple 2p card game app using node.js and socket.io

No other libraries like react etc.

Client starts a table and waits for a second person to join the table. When the number of clients in the table is 2, I emit a ‘readyToPlay’ message from the server to the client who started the table.

Then on the client I capture the ‘readyToPlay’ message and in the callback I update the page (only for the person who started the table) with a ‘start game’ button"… The button pressed calls a client function startGame() which emits ‘createGame’ to server. Server catches this and builds the deck and array of player hands etc.

The game is simple, only made up of 18 cards and some tokens so I store the full game state (cards in play and player hands) in an array on the server.

Then server sends socket messages ‘updatePublic’ to players to pass data and call a client function which updates the cards on the playing area, then same for ‘updatePrivate’ to update their secret hands.

Am I using sockets correctly? Something about this seems clumsy.

Add Comment
1 Answer(s)

Is telling a client to do minor html updates appropriate use of socket.io?

Yes. This is a perfectly fine use of socket.io. Any time you need to "push" data from the server to the client, then socket.io often makes a lot of sense.

It is a design choice whether you push JSON data or you push HTML. I most cases, you just push the data and let the front-end code take care of how to render that data into the page appropriately. This way, the entire presentation is controlled in one place rather than have presentation code mixed in with the socket.io updates. But, that’s a design choice. Either can work.

Am I using sockets correctly?

You don’t show us your code, but the concept for what you’re doing is perfectly fine.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.