Skip to content

Instantly share code, notes, and snippets.

@timsuchanek
Last active March 2, 2017 22:23
Show Gist options
  • Save timsuchanek/4db1c5beb55a51f7f659be66bad0c8a4 to your computer and use it in GitHub Desktop.
Save timsuchanek/4db1c5beb55a51f7f659be66bad0c8a4 to your computer and use it in GitHub Desktop.
Observe Subscriptions
import { Observable } from 'rxjs/Observable'
import { SubscriptionClient } from 'subscriptions-transport-ws'
const ws = new SubscriptionClient("endpointurl")
const observable = Observable.create(observer => {
const id = this.ws.subscribe(graphQLParams, (err, res) => {
if (err) {
observer.next(res)
observer.unsubscribe()
}
observer.next(res)
})
return () => {
ws.unsubscribe(id)
}
})
const subscription = observable.subscribe({
next: data => {
console.log('data', data)
},
error: error => {
console.error(error)
},
complete: () => {
console.log('subscription complete')
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment