Skip to content

Instantly share code, notes, and snippets.

@timsuchanek
Created November 15, 2017 14:11
Show Gist options
  • Save timsuchanek/3a01f4b7246091ebe107f0638918137a to your computer and use it in GitHub Desktop.
Save timsuchanek/3a01f4b7246091ebe107f0638918137a to your computer and use it in GitHub Desktop.
Minimal GraphQL Subscription working with subscriptions-transport-ws 0.9.1
<html>
<head>
<title>Websockets Test</title>
</head>
<body>
<script>
const socket = new WebSocket(
'wss://subscriptions.graph.cool/v1/cirs1ufsg02b101619ru0bx5r','graphql-ws'
)
socket.addEventListener('open', event => {
console.log('open')
console.log(event)
socket.send(JSON.stringify({type: 'connection_init'}))
})
socket.addEventListener('message', event => {
console.log('message')
console.log(event)
const data = JSON.parse(event.data)
if (data.type === 'connection_ack') {
const request = {
type: 'start',
id: '0',
payload: {
query: `subscription {
B {
node {
id
}
}
}`
}
}
socket.send(JSON.stringify(request))
}
})
socket.addEventListener('error', event => {
console.log('error')
console.log(event)
})
</script>
</body>
</html>
@marktani
Copy link

see https://gist.github.com/marktani/5df524523693c88be425bfb623ca8b8a for an example with the Authorization connection parameter

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