{"id":24481008,"url":"https://github.com/niradler/socketflow","last_synced_at":"2025-03-14T18:18:34.386Z","repository":{"id":270908167,"uuid":"909820619","full_name":"niradler/socketflow","owner":"niradler","description":"SocketFlow is a lightweight, efficient, and easy-to-use WebSocket library for Go.","archived":false,"fork":false,"pushed_at":"2025-03-06T00:22:57.000Z","size":25,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-06T01:24:30.538Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/niradler.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-29T21:43:23.000Z","updated_at":"2025-03-06T00:13:33.000Z","dependencies_parsed_at":"2025-01-04T00:23:05.432Z","dependency_job_id":"30ed4179-0f5c-41e1-8c45-428eea974498","html_url":"https://github.com/niradler/socketflow","commit_stats":null,"previous_names":["niradler/socketflow"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niradler%2Fsocketflow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niradler%2Fsocketflow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niradler%2Fsocketflow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niradler%2Fsocketflow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/niradler","download_url":"https://codeload.github.com/niradler/socketflow/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243624103,"owners_count":20321029,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-21T11:18:36.970Z","updated_at":"2025-03-14T18:18:34.364Z","avatar_url":"https://github.com/niradler.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SocketFlow\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/niradler/socketflow.svg)](https://pkg.go.dev/github.com/niradler/socketflow)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/niradler/socketflow/blob/main/LICENSE)\n\nSocketFlow is a lightweight, efficient, and easy-to-use WebSocket library for Go. It supports **topic-based messaging**, **automatic chunking for large payloads**, and **real-time two-way communication**. Designed with simplicity and performance in mind, SocketFlow is perfect for building real-time applications like chat systems, live updates, and more.\n\n## Features\n\n- **Topic-Based Messaging**: Send and receive messages based on topics.\n- **Automatic Chunking**: Automatically splits large payloads into chunks and reassembles them on the receiving end.\n- **Real-Time Two-Way Communication**: Supports both request-response and pub/sub patterns.\n- **Thread-Safe**: Uses mutexes to ensure thread safety.\n- **Easy-to-Use API**: Simple and intuitive API for sending, receiving, and subscribing to messages.\n\n## Installation\n\n```bash\ngo get github.com/niradler/socketflow\n```\n\n## Usage\n\n### Server\n\nCreate a WebSocket server that handles incoming connections and messages:\n\n```go\npackage main\n\nimport (\n    \"log\"\n    \"net/http\"\n\n    \"github.com/gorilla/websocket\"\n    \"github.com/niradler/socketflow\"\n)\n\nvar upgrader = websocket.Upgrader{\n    CheckOrigin:       func(r *http.Request) bool { return true },\n    EnableCompression: true,\n}\n\nfunc handleWebSocket(w http.ResponseWriter, r *http.Request) {\n    conn, err := upgrader.Upgrade(w, r, nil)\n    if err != nil {\n        log.Fatal(\"Failed to upgrade WebSocket connection:\", err)\n    }\n    defer conn.Close()\n\n    conn.EnableWriteCompression(true)\n    client := socketflow.NewWebSocketClient(conn, socketflow.Config{\n        ChunkSize: 1024,\n    })\n\n    ch := client.Subscribe(\"test-topic\")\n    go func() {\n        for msg := range ch {\n            log.Printf(\"Received topic message: ID=%s, Topic=%s, PayloadLen=%v\\n\", msg.ID, msg.Topic, len(msg.Payload))\n            log.Println(\"Payload:\", string(msg.Payload))\n        }\n    }()\n\n    client.ReceiveMessages()\n}\n\nfunc main() {\n    http.HandleFunc(\"/ws\", handleWebSocket)\n    log.Println(\"WebSocket server started at :8080\")\n    log.Fatal(http.ListenAndServe(\"localhost:8080\", nil))\n}\n```\n\n### Client\n\nCreate a WebSocket client that connects to the server and sends/receives messages:\n\n```go\npackage main\n\nimport (\n    \"bytes\"\n    \"log\"\n    \"time\"\n\n    \"github.com/gorilla/websocket\"\n    \"github.com/niradler/socketflow\"\n)\n\nfunc main() {\n    conn, _, err := websocket.DefaultDialer.Dial(\"ws://localhost:8080/ws\", nil)\n    if err != nil {\n        log.Fatal(\"Failed to connect to WebSocket server:\", err)\n    }\n    defer conn.Close()\n\n    client := socketflow.NewWebSocketClient(conn, socketflow.Config{\n        ChunkSize: 1024,\n    })\n\n    conn.EnableWriteCompression(true)\n    statusChan := client.SubscribeToStatus()\n    go func() {\n        for status := range statusChan {\n            log.Printf(\"Received status: %v\\n\", status)\n        }\n    }()\n\n    client.TrackMetrics(time.Minute * 1)\n    client.StartHeartbeat(time.Second * 15)\n\n    id, err := client.SendMessage(\"test-topic\", []byte(\"Hello, World!\"))\n    if err != nil {\n        log.Fatal(\"Failed to send message:\", err)\n    }\n    log.Printf(\"Sent small message with ID: %s\\n\", id)\n\n    pattern := []byte(\"abcd\")\n    largePayload := bytes.Repeat(pattern, 1500/len(pattern))\n    id, err = client.SendMessage(\"test-topic\", []byte(largePayload))\n    if err != nil {\n        log.Fatal(\"Failed to send message:\", err)\n    }\n    log.Printf(\"Sent large message with ID: %s\\n\", id)\n\n    ch := client.Subscribe(\"test-topic\")\n    go func() {\n        for msg := range ch {\n            log.Printf(\"Received message: ID=%s, Topic=%s, Payload=%s\\n\", msg.ID, msg.Topic, msg.Payload)\n        }\n    }()\n\n    client.ReceiveMessages()\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniradler%2Fsocketflow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fniradler%2Fsocketflow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniradler%2Fsocketflow/lists"}