Created
December 15, 2017 15:48
-
-
Save marktani/5df524523693c88be425bfb623ca8b8a to your computer and use it in GitHub Desktop.
Replace __PROJECT_ID__ and __TOKEN__ and adjust QUERY
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Websockets Test</title> | |
</head> | |
<body> | |
<script> | |
const socket = new WebSocket( | |
'wss://subscriptions.graph.cool/v1/__PROJECT_ID__','graphql-ws' | |
) | |
socket.addEventListener('open', event => { | |
console.log('open') | |
console.log(event) | |
socket.send(JSON.stringify({ | |
type: 'connection_init', | |
payload: { | |
Authorization: `Bearer __TOKEN__` | |
} | |
})) | |
}) | |
const QUERY = ` | |
subscription { | |
Post { | |
node { | |
id | |
} | |
} | |
} | |
` | |
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: QUERY | |
} | |
} | |
socket.send(JSON.stringify(request)) | |
} | |
}) | |
socket.addEventListener('error', event => { | |
console.log('error') | |
console.log(event) | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment