r/Clojure • u/a-curious-crow • Feb 04 '23
Syncing client/server state with CLJS/Clojure, ring, and react/re-frame
I'm working on a digital multiplayer (turn based) game, which I'm basing off of this template project: https://github.com/schnaq/cljs-re-frame-full-stack.
I'm at a place where the majority of my game logic is in the cljs client and I want to start syncing this state to the server, where it can then be sent to the other clients. My initial thoughts on how to do this are for a client to send the game state to the server via a :post request after it completes a turn. The clients whose turn it isn't would constantly poll the server until it gets this :post and sends them all the new state. Then the client whose turn it has become would start the cycle over again.
Before I start implementing this I wanted to ask if there are any pre build solutions for this kind of system that I should use instead. Even keywords to help me know what to search for would be much appreciated!
2
u/a-curious-crow Feb 04 '23
Thanks all, looks like sente should work quite well! I'll try it out.
7
u/arthurbarroso Feb 04 '23
you’re probably going to want to use websockets for the state syncing/polling. googling “re-frame websockets” should net you some results. “web development with clojure (third edition)” by yogthos also shows how to do it in clojure(script) iirc
2
u/xela314159 Feb 04 '23
This would work, but if you want something more real time for the player whose turn it isn’t, so you don’t have to poll the server through get requests constantly, you can look at a socket connection. Sente is a library that does that.
1
u/maxw85 Feb 04 '23
This is a pretty interesting approach for this problem:
https://blog.codefrau.net/2021/08/what-is-croquet-anyways.html?m=1
1
u/Wassaren Feb 04 '23
I did something which sounds very similar to this.
https://github.com/Antracen/Clojure-ChessServer/blob/main/src/chess_server/server.clj
I am a total beginner in Clojure, so there is a very high probability that I have done something weird / bad praxis, but perhaps it could give you some inspiration. I used websockets.