{"id":18588536,"url":"https://github.com/racerxdl/go-subscription-handler","last_synced_at":"2025-04-10T14:30:47.211Z","repository":{"id":57571809,"uuid":"238352129","full_name":"racerxdl/go-subscription-handler","owner":"racerxdl","description":"Golang Simple GraphQL Subscriptions Handler","archived":false,"fork":false,"pushed_at":"2020-02-05T03:31:29.000Z","size":15,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-24T22:42:18.669Z","etag":null,"topics":["golang","graphql","subscriptions","websocket"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/racerxdl.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}},"created_at":"2020-02-05T02:32:23.000Z","updated_at":"2021-07-26T05:33:56.000Z","dependencies_parsed_at":"2022-08-23T17:31:15.984Z","dependency_job_id":null,"html_url":"https://github.com/racerxdl/go-subscription-handler","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/racerxdl%2Fgo-subscription-handler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/racerxdl%2Fgo-subscription-handler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/racerxdl%2Fgo-subscription-handler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/racerxdl%2Fgo-subscription-handler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/racerxdl","download_url":"https://codeload.github.com/racerxdl/go-subscription-handler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248233946,"owners_count":21069493,"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":["golang","graphql","subscriptions","websocket"],"created_at":"2024-11-07T00:46:20.154Z","updated_at":"2025-04-10T14:30:46.890Z","avatar_url":"https://github.com/racerxdl.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Golang Simple GraphQL Subscription Handler\n\n## Usage\n\nSubscription node:\n\n```go\nvar rootSubscriptions = graphql.ObjectConfig{\n    Name: \"RootSubscriptions\",\n    Fields: graphql.Fields{\n        \"serverTime\": \u0026graphql.Field{\n            Type: graphql.Float,\n            Resolve: func(p graphql.ResolveParams) (interface{}, error) {\n                err := subhandler.Subscribe(p.Context, \"serverTime\")\n\n                if p.Source != nil {\n                    // We received a event data\n                    v, ok := p.Source.(map[string]interface{})\n                    if ok \u0026\u0026 v[\"time\"] != nil {\n                        return v[\"time\"], nil\n                    }\n                }\n\n                // We didn't receive a event data, so resolve normally\n                return time.Now().String(), err\n            },\n        },\n    },\n}\n\nvar rootQuery = graphql.ObjectConfig{\n    Name: \"RootQuery\",\n    Fields: graphql.Fields{\n        \"serverTime\": \u0026graphql.Field{\n            Type: graphql.Float,\n            Resolve: func(p graphql.ResolveParams) (interface{}, error) {\n                return time.Now().Unix(), nil\n            },\n        },\n    },\n}\n\n\nvar schemaConfig = graphql.SchemaConfig{\n    Query:        graphql.NewObject(rootQuery),\n    Subscription: graphql.NewObject(rootSubscriptions),\n}\n\nfunc GetSchema() (graphql.Schema, error) {\n    return graphql.NewSchema(schemaConfig)\n}\n\n```\n\nMain Code:\n\n```go\npackage main\n\nimport (\n    \"github.com/asaskevich/EventBus\"\n    \"github.com/graphql-go/handler\"\n    \"github.com/racerxdl/go-subscription-handler/subhandler\"\n    \"net/http\"\n    \"time\"\n    \"fmt\"\n)\n\n// Create a notifier\ntype BusNotifier struct {\n    bus EventBus.Bus // You can use any pub-sub lib for that\n}\n\nfunc MakeBusNotifier(bus EventBus.Bus) *BusNotifier {\n    return \u0026BusNotifier{\n        bus: bus,\n    }\n}\n\nfunc (bn *BusNotifier) Subscribe(topic string, cb func(data map[string]interface{})) {\n    bn.bus.Subscribe(topic, cb)\n}\n\nfunc (bn *BusNotifier) Unsubscribe(topic string, cb func(data map[string]interface{})) {\n    bn.bus.Unsubscribe(topic, cb)\n}\n\nfunc (bn *BusNotifier) Notify(topic string, data map[string]interface{}) {\n    bn.bus.Publish(topic, data)\n}\n\n// Initialize a Sub Handler\nfunc main() {\n\n    schema, _ := GetSchema()\n\n    // Create normal mutation / query handlers\n    h := handler.New(\u0026handler.Config{\n        Schema:     \u0026schema,\n        Pretty:     true,\n        Playground: true,\n    })\n    \n    // Initialize our notifier\n    notifier := MakeBusNotifier(EventBus.New())\n\n    // Create a goroutine to send notifications through notifier\n    go func() {\n        for {\n            data := map[string]interface{}{\n                \"time\": time.Now().String(),\n            }\n            notifier.Notify(\"serverTime\", data)\n            time.Sleep(time.Second) // Sleep some interval\n\n            // This also works, makes the resolver decide what to do\n            notifier.Notify(\"serverTime\", nil)\n        }\n    }()\n\n    // Create the subscription handler using notifier and schema\n    sh := subhandler.MakeSubscriptionHandler(notifier, schema)\n\n    // Optional, check origin of the request\n    sh.SetCheckOrigin(func(r *http.Request) bool {\n        // Accept any origin\n        return true\n    })\n\n    // Attach the normal query / mutation handlers\n    http.Handle(\"/\", h)\n    \n    // Attach the subscription handlers\n    http.Handle(\"/subscriptions\", sh)\n    \n    fmt.Println(\"Listening in :8080\")\n    http.ListenAndServe(\":8080\", nil)\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fracerxdl%2Fgo-subscription-handler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fracerxdl%2Fgo-subscription-handler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fracerxdl%2Fgo-subscription-handler/lists"}