{"id":19372700,"url":"https://github.com/hasura/nodejs-graphql-subscriptions-boilerplate","last_synced_at":"2025-04-23T16:33:19.406Z","repository":{"id":46584394,"uuid":"142329909","full_name":"hasura/nodejs-graphql-subscriptions-boilerplate","owner":"hasura","description":"Boilerplate to setup GraphQL subscriptions in your nodejs code","archived":false,"fork":false,"pushed_at":"2019-11-12T19:39:17.000Z","size":19,"stargazers_count":79,"open_issues_count":5,"forks_count":9,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-04-14T11:41:35.241Z","etag":null,"topics":["boilerplate","graphql","graphql-subscriptions","nodejs"],"latest_commit_sha":null,"homepage":"https://hasura.io","language":"JavaScript","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/hasura.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":"2018-07-25T17:03:33.000Z","updated_at":"2024-03-23T11:57:20.000Z","dependencies_parsed_at":"2022-08-28T16:22:47.894Z","dependency_job_id":null,"html_url":"https://github.com/hasura/nodejs-graphql-subscriptions-boilerplate","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/hasura%2Fnodejs-graphql-subscriptions-boilerplate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hasura%2Fnodejs-graphql-subscriptions-boilerplate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hasura%2Fnodejs-graphql-subscriptions-boilerplate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hasura%2Fnodejs-graphql-subscriptions-boilerplate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hasura","download_url":"https://codeload.github.com/hasura/nodejs-graphql-subscriptions-boilerplate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223929717,"owners_count":17226944,"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":["boilerplate","graphql","graphql-subscriptions","nodejs"],"created_at":"2024-11-10T08:25:00.099Z","updated_at":"2024-11-10T08:25:00.715Z","avatar_url":"https://github.com/hasura.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Boilerplate to test GraphQL subscriptions using nodejs\nAlthough it takes just a [few lines of code](./index.js#L1-L21) to set up your NodeJS code to use GraphQL subscriptions, it does involve going through a few pages of docs. Here's a few lines of boilerplate you can use to get started quickly (copy-paste 🤘).\n\n## Usage:\n\n- Reference code:\n  Refer to [index.js](./index.js#L1-L21) for the boilerplate code that sets up a GraphQL subscription observable.\n\n\n#### Step 1: Setup the subscription client\n\n  This is all the code you need\n```javascript\n\nconst { execute } = require('apollo-link');\nconst { WebSocketLink } = require('apollo-link-ws');\nconst { SubscriptionClient } = require('subscriptions-transport-ws');\nconst ws = require('ws');\n\nconst getWsClient = function(wsurl) {\n  const client = new SubscriptionClient(\n    wsurl, {reconnect: true}, ws\n  );\n  return client;\n};\n\nconst createSubscriptionObservable = (wsurl, query, variables) =\u003e {\n  const link = new WebSocketLink(getWsClient(wsurl));\n  return execute(link, {query: query, variables: variables});\n};\n```\n\n#### Step 2: Use it\n\n```javascript\nconst gql = require('graphql-tag');\n// A subscription query to get changes for author with parametrised id \n// using $id as a query variable\nconst SUBSCRIBE_QUERY = gql`\nsubscription liveAuthor($id: Int!) {\n  author (where: {id: {_eq: $id}}) {\n    id\n    name\n  }\n}\n`;\n\nconst subscriptionClient = createSubscriptionObservable(\n  'https://test-gql-sub.herokuapp.com/v1alpha1/graphql', // GraphQL endpoint\n  SUBSCRIBE_QUERY,                                       // Subscription query\n  {id: 1}                                                // Query variables\n);\nvar consumer = subscriptionClient.subscribe(eventData =\u003e {\n  // Do something on receipt of the event\n  console.log(\"Received event: \");\n  console.log(JSON.stringify(eventData, null, 2));\n}, (err) =\u003e {\n  console.log('Err');\n  console.log(err);\n});\n```\n\n#### Dependencies:\n\n  This uses the [apollo link](https://github.com/apollographql/apollo-link), [subscription-transport-ws](https://github.com/apollographql/subscriptions-transport-ws) libraries.\n  \n```\nnpm install --save graphql graphql-tag apollo-link apollo-link-ws subscriptions-transport-ws ws\n```\n\n--------------------\n\n## Try it out:\n\n- Use [hasura](https://hasura.io) as a test backend: [![Deploy to Heroku](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/hasura/graphql-engine-heroku)\n- Create a table `author` with columns `id: integer`, `name: text`\n- Set the GraphQL endpoint in `index.js` to `https://HEROKU_APP_NAME.herokuoapp.com/v1alpha1/graphql`\n- `git clone https://github.com/hasura/nodejs-graphql-subscriptions-boilerplate.git`\n- `cd nodejs-graphql-subscriptions-boilerplate`\n- `npm i`\n- `node index.js`\n- Now head to the Hasura console and insert an author with id: 1, name: honeysingh\n- Changes will appear on your nodejs code\n- Edit the row on the console\n- Changes will appear on your nodejs code\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhasura%2Fnodejs-graphql-subscriptions-boilerplate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhasura%2Fnodejs-graphql-subscriptions-boilerplate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhasura%2Fnodejs-graphql-subscriptions-boilerplate/lists"}