Skip to content

Instantly share code, notes, and snippets.

@zacck-zz
Created May 28, 2019 17:52
Show Gist options
  • Save zacck-zz/1fe017588a00cd0ada6b412e38221338 to your computer and use it in GitHub Desktop.
Save zacck-zz/1fe017588a00cd0ada6b412e38221338 to your computer and use it in GitHub Desktop.
const AbsintheSocket = require("@absinthe/socket");
const {Socket: PhoenixSocket} = require("phoenix");
// establish a connection
const conSock = new PhoenixSocket("ws://localhost:4000/socket");
//build an absinthe socket
const abSocket = AbsintheSocket.create(conSock);
const sub = `
subscription{
salesOperation {
name
units
}
}
`;
console.log(sub)
subscribe(sub)
function subscribe(doc){
//create fresh notifiers for this document
notifier = AbsintheSocket.send(abSocket, {
doc,
variables: {}
});
result = AbsintheSocket.observe(abSocket, notifier, {
onAbort,
onError,
onCancel,
onStart,
onResult
});
console.log(result)
return result;
}
// this handles a successful subscription registration, a good point to tell the user to wait
function onStart(data) {
console.log(">>> Start", JSON.stringify(data));
}
//handle connection failure from the client side, inform the user of connection issues
function onAbort(data) {
console.log(">>> Abort", JSON.stringify(data));
}
//handle error occurence when registering listener, inform the user of subscription failure
function onError(data) {
console.log(">>> Error", JSON.stringify(data));
}
//handle connection cancel
function onCancel(data) {
console.log(">>> Cancel", JSON.stringify(data));
}
//handle result from subscription, inform the user of the result from the subscription
function onResult(res) {
console.log(">>> Result", JSON.stringify(res));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment