{"id":13565349,"url":"https://github.com/graphql-java/graphql-java-subscription-example","last_synced_at":"2025-05-01T15:30:23.896Z","repository":{"id":26644054,"uuid":"106741890","full_name":"graphql-java/graphql-java-subscription-example","owner":"graphql-java","description":"An example of graphql-java and subscriptions","archived":false,"fork":false,"pushed_at":"2021-04-07T16:34:11.000Z","size":74,"stargazers_count":65,"open_issues_count":1,"forks_count":32,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-28T08:25:24.224Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","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/graphql-java.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":"2017-10-12T20:28:12.000Z","updated_at":"2025-01-05T19:47:58.000Z","dependencies_parsed_at":"2022-08-07T12:00:49.504Z","dependency_job_id":null,"html_url":"https://github.com/graphql-java/graphql-java-subscription-example","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/graphql-java%2Fgraphql-java-subscription-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-java%2Fgraphql-java-subscription-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-java%2Fgraphql-java-subscription-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-java%2Fgraphql-java-subscription-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/graphql-java","download_url":"https://codeload.github.com/graphql-java/graphql-java-subscription-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251898494,"owners_count":21661837,"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":"2024-08-01T13:01:45.216Z","updated_at":"2025-05-01T15:30:23.874Z","avatar_url":"https://github.com/graphql-java.png","language":"Java","readme":"# graphql-java Subscriptions over WebSockets example\n\nAn example of using graphql subscriptions via websockets, graphql-java, reactive-streams and RxJava.\n\nTo build the example code in this repository type:\n\n    ./gradlew build\n    \nTo run the example code type:\n    \n    ./gradlew run\n    \nTo access the example application, point your browser at:\n\n    http://localhost:3000/  \n    \n# Code Explanation\n\nThis example shows how you can use graphql-java subscription support to \"subscribe\" to a publisher of events.\nThen as events occur, graphql-java will map the original graphql query over those same event objects and send out\na stream of `ExecutionResult` objects.\n\nIn this example application we have a stock update type system defined as:\n\n    type Subscription {\n        stockQuotes(stockCodes:[String]) : StockPriceUpdate!\n    }\n    \n    type StockPriceUpdate {\n        dateTime : String\n        stockCode : String\n        stockPrice : Float\n        stockPriceChange : Float\n    }\n\nThe JavaScript client sends a subscription graphql query over websockets to the server:\n\n    var query = 'subscription StockCodeSubscription { \\n' +\n        '    stockQuotes {' +\n        '       dateTime\\n' +\n        '       stockCode\\n' +\n        '       stockPrice\\n' +\n        '       stockPriceChange\\n' +\n        '     }' +\n        '}';\n    var graphqlMsg = {\n        query: query,\n        variables: {}\n    };\n    exampleSocket.send(JSON.stringify(graphqlMsg));\n   \nThe server executes this with the graphql-java engine:\n\n        GraphQL graphQL = GraphQL\n                .newGraphQL(graphqlPublisher.getGraphQLSchema())\n                .build();\n\n        ExecutionResult executionResult = graphQL.execute(executionInput);\n\nThe result of that initial subscription query is a http://www.reactive-streams.org/ `Publisher`\n\n        Publisher\u003cExecutionResult\u003e stockPriceStream = executionResult.getData();\n        \nUnder the covers a RxJava 2.x implementation is used to provide a stream of synthesized stock events.\n\nRxjava Flows are an implementation of the reactive streams Publisher interface.  You can use ANY reactive streams\nimplementation as a source.  graphql-java uses the reactive streams interfaces as a common interface.\n\nSee https://github.com/ReactiveX/RxJava for more information on RxJava.  \n        \nThe server side code then subscribes to this publisher of events and sends the results back over the websocket\nto the waiting browser client:\n\n        stockPriceStream.subscribe(new Subscriber\u003cExecutionResult\u003e() {\n\n            @Override\n            public void onSubscribe(Subscription s) {\n                subscriptionRef.set(s);\n                request(1);\n            }\n\n            @Override\n            public void onNext(ExecutionResult er) {\n                log.debug(\"Sending stick price update\");\n                try {\n                    Object stockPriceUpdate = er.getData();\n                    getRemote().sendString(JsonKit.toJsonString(stockPriceUpdate));\n                } catch (IOException e) {\n                    e.printStackTrace();\n                }\n                request(1);\n            }\n\n            @Override\n            public void onError(Throwable t) {\n                log.error(\"Subscription threw an exception\", t);\n                getSession().close();\n            }\n\n            @Override\n            public void onComplete() {\n                log.info(\"Subscription complete\");\n                getSession().close();\n            }\n        });\n\nThe selection set of fields named in the original query will be applied to each underlying stock update object.  \n\nThe selection set in this example application is selected as follows:\n\n        stockQuotes {\n            dateTime\n            stockCode\n            stockPrice\n            stockPriceChange\n        }\n\nThe underling stock update object is mapped to this selection of fields, just like any normal graphql query.  The format\nof the results on the browser is JSON, again like any other normal graphql query.\n","funding_links":[],"categories":["Java"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphql-java%2Fgraphql-java-subscription-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgraphql-java%2Fgraphql-java-subscription-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphql-java%2Fgraphql-java-subscription-example/lists"}