Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@stubailo
Created October 2, 2016 16:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stubailo/8b7ff3b60221e0d648a50f9bc8065e89 to your computer and use it in GitHub Desktop.
Save stubailo/8b7ff3b60221e0d648a50f9bc8065e89 to your computer and use it in GitHub Desktop.
Use subscriptions-transport-ws to run a GraphQL subscription from a JavaScript client.
// To run this code yourself:
// 1. Download and run the demo server: https://github.com/apollostack/frontpage-server
// 2. Use create-react-app to create a build system
// 3. npm install subscriptions-transport-ws
// 4. Replace all of the code in src/ with just this file
import { Client } from 'subscriptions-transport-ws';
const client = new Client('ws://localhost:8090');
client.subscribe({
query: `
subscription UpvotedPostSubscription {
postUpvoted {
title
votes
}
}
`
}, (err, res) => {
if (err) {
console.error(err);
return;
}
console.log(`"${res.postUpvoted.title}" has ${res.postUpvoted.votes} votes now.`);
})
@anhldbk
Copy link

anhldbk commented Nov 28, 2016

Well, I've got an error of:

"Schema must be an instance of GraphQLSchema. Also ensure that there are not multiple versions of GraphQL installed in your node_modules directory."

Still I don't know how to use subscriptions. :'( My issue is here: https://github.com/apollostack/GitHunt-React/issues/156

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment