'Managing multiple shared states between pairs of users
I'm fairly new to React/React Native and I've built a pretty basic app that manages the state of board game characters for two players. The characters are all stored in an array with useState - there are two screens, one that shows player one's characters, and another that shows player two's. I have the following requirements:
- A user (player one) can create a hosted 'game' so that player two can connect.
- Player two is able to find and connect to the same game as that player (we can assume they will be on the same network).
- Player one is able to edit/rename/delete the characters on the player one screen.
- Player two is able to do the same as above for the player two screen.
- Any changes made by either player are updated on both player's devices.
- Multiple games may need to be managed at the same time, depending on the number of pairs of players using the app.
Can anyone recommend the best high-level approach for achieving this?
Solution 1:[1]
For your objective I would like you use a library like socket.io.
socket.io creates a connection between two clients and the data can be interchanged between both clients with use of events.
One player's device will emit a event and other will recieve the event containing current state or data. You can trigger events for changes in game and update those in other device from recieved data.
This is just and overview If you decide to use socket.io I would recommend you to go through it's documentation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anurag Parmar |
