You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 17, 2021. It is now read-only.
import{Subscription}from"sse-z";constsubscription=newSubscription({url: "https://localhost:8080/sse",searchParams: {foo: "bar",},onNext: (data: string)=>{console.log(data);},});// stop the subscriptionsubscription.unsubscribe();
Types
classSubscription{eventSource: EventSource;constructor(options: SSESubscriptionOptions);unsubscribe(): void;}interfaceSubscriptionOptions{// Additional options to pass to the constructor of the underlying EventSource instance.eventSourceOptions?: {withCredentials?: boolean;[key: string]: any;};// Indicates the subscription should expect keep alive events to be sent by the server.// If an event is not received inside the provided interval, a reconnection attempt will be made.// The provided interval should be greater than the actual frequency at which the server sends// the events to allow for network latency.keepAlive?: {// Defaults to "keepAlive";eventType?: string;intervalMs: number;};// Called when the connection is terminated by calling unsubscribe.onComplete?: ()=>void;// Called when an error occurs. Note that this callback will be called each time the connection// is lost, so it should not be used to indicate a critical error occurred.onError?: (error: Error)=>void;// Callback called whenever an event is pushed.onNext?: (data: string)=>void;// Any URL query parameters to attach to the URL.searchParams?: {[key: string]: string;};// The URL of the endpoint to fetch requests from.url: string;}
Integration with GraphQL Clients
Relay
import{Environment,Network,Observable,SubscribeFunction,}from"relay-runtime";import{Subscription}from"sse-z";constsubscribe: SubscribeFunction=(operation,variables)=>{returnObservable.create((sink)=>{returnnewSubscription({url: 'https://localhost:8080/graphql',searchParams: {operationName: operation.name,query: operation.text,variables: JSON.stringify(variables),},eventSourceOptions: {// Ensure cookies are included with the requestwithCredentials: true,},onNext: (data)=>{sink.next(JSON.parse(data));},});});};constenvironment=newEnvironment({
...
network: Network.create(fetchQuery,subscribe),});
Apollo Client
import{ApolloLink,Operation,FetchResult,Observable}from"@apollo/client";import{print}from"graphql";import{Subscription,SubscriptionOptions}from"sse-z";classSSELinkextendsApolloLink{options: SSESubscriptionOptions;constructor(options: SubscriptionOptions){super();this.options=options;}publicrequest({
query,
variables,
operationName,}: Operation): Observable<FetchResult>{returnnewObservable((sink)=>{constsubscription=newSubscription({
...options,searchParams: {query: print(operation.query),variables: JSON.stringify(variables),
operationName,},onNext: (data)=>{sink.next(JSON.parse(data));},});return()=>subscription.unsubscribe();});}}constlink=newSSELink({url: "https://localhost:8080/graphql",eventSourceOptions: {// Ensure cookies are included with the requestwithCredentials: true,},});