This is a backend service written in Golang using Gin and WebSocket to keep JSON data synchronized between clients. It uses Redis for persistence, ensuring quick response times. The service allows clients to upload JSON data, receive updates from other clients via WebSocket, and query the entire data. This is ideal for applications that require real-time synchronization and data consistency across multiple clients.
- Gin: A high-performance web framework for Go.
- WebSocket: For real-time bidirectional communication.
- Redis: In-memory database for fast data storage and retrieval.
- Golang: A statically typed, compiled programming language designed at Google.
-
Clone the Repository
git clone https://github.com/Vadim-Karpenko/golang-json-sync-service.git cd golang-json-sync-service
-
Install Dependencies
Ensure you have Go installed. Then, use the following command to install the necessary Go packages:
go mod tidy
-
Run Redis
Make sure you have Redis installed and running. You can use Docker to run Redis:
docker run -d -p 6379:6379 redis
-
Run the Server
Run the server using:
go run
The server will start on
http://localhost:8080
. -
Build (Optional)
If you want to build the executable, use:
go build
- Endpoint:
POST /upload
- Request Body: Any JSON data
- Response:
{ "uuid": "unique-identifier" }
Just so you know, the default timeout for the JSON data is 30 days after the last update. After that, it will be deleted from the Redis database. Edit the source code if you want to change this behavior.
- Endpoint:
ws://localhost:8080/ws/:uuid
(use wss:// if in production!) - Message Body:
{ "path": "path.to.value.that.changed", "value": "new value" }
- Response:
{ "path": "path.to.value.that.changed", "value": "new value" }
If you have a list of items in your JSON, you can use the index to update the specific item. The path should look like this: character.items.1.name
to update the name of the second item in the list. Value could be of any type (list, dict, string, etc.).
- Endpoint:
GET /json/:uuid
- Response: JSON data
To run the tests for this project, execute:
go test
-
Upload JSON Data
curl -X POST http://localhost:8080/upload -d '{"character": {"name": "Frodo", "age": 50, "items": ["cloak", "ring"]}}' -H "Content-Type: application/json"
-
Connect via WebSocket
Use a WebSocket client to connect to
ws://localhost:8080/ws/{uuid}
and send updates.{"path": "character.age", "value": 51}
Other connected users will receive the exact message you sent for an update. Use it to update the locally stored data in your application.
-
Retrieve Updated JSON (optional)
If it's too complicated in your case to update individual keys, you can just retrieve the whole thing by using:
curl http://localhost:8080/json/{uuid}
Also, if your user just got connected, it would be a good idea to use this API during the init phase.
Contributions are welcome! Please submit issues and pull requests on GitHub.
For any questions or support, please contact [email protected].