Implement the WebSocket library using the Gin framework and the Gorilla WebSocket library in Go.
This repository provides a simple WebSocket implementation using the Gin framework and Gorilla WebSocket library in Go. It allows real-time communication between clients and a server through WebSocket connections.
- Topic-based Subscription: Clients can subscribe to specific topics, and the server will broadcast messages to all subscribers of that topic.
- Dynamic Topic Registration: Topics can be dynamically registered by clients, allowing for flexible and dynamic communication channels.
- Concurrency Handling: The implementation uses Gorilla WebSocket and supports concurrent connections and message broadcasting.
- Closure Handling: Optionally, the server can be configured to handle closure events, such as detecting when a client connection is closed.
Golang version v1.20
- Latest version
go get -u github.com/sivaosorg/wsconn@latest- Use a specific version (tag)
go get github.com/sivaosorg/[email protected]Explain how users can interact with the various modules.
To run tests for all modules, use the following command:
make testTo tidy up the project's Go modules, use the following command:
make tidyTo upgrade project dependencies, use the following command:
make deps-upgradeTo clean the Go module cache, use the following command:
make deps-clean-cacheConnect to the WebSocket server using a WebSocket client. For example, in a browser, you can use JavaScript or tools like WebSocket.org's WebSocket Tester.
const socket = new WebSocket("ws://localhost:8080/subscribe");
// Handle connection open event
socket.addEventListener("open", (event) => {
  console.log("WebSocket connection opened:", event);
  // Subscribe to a topic
  const subscription = {
    topic: "your-topic-name",
    content: "sample",
    userId: "user123",
    isPersistent: true,
    // Add any additional subscription parameters as needed
  };
  socket.send(JSON.stringify(subscription));
});
// Handle incoming messages
socket.addEventListener("message", (event) => {
  const message = JSON.parse(event.data);
  console.log("Received message:", message);
});
// Handle connection close event
socket.addEventListener("close", (event) => {
  console.log("WebSocket connection closed:", event);
});
// Handle connection error event
socket.addEventListener("error", (event) => {
  console.error("WebSocket error:", event);
});You can dynamically register topics using a RESTful API endpoint:
curl -X POST http://localhost:8080/register -d '{"topic": "your-topic"}'