2.0.0 • Published 1 year ago
@mercuriusjs/subscription-client v2.0.0
@mercuriusjs/subscription-client
A websocket client for Mercurius subscriptions.
Quick start
npm i @mercuriusjs/subscription-clientSubscriptionClient
SubscriptionClient: class extends EventEmitter
The SubscriptionClient class constructor takes two parameters:
uri:stringThe uri of the websocket server.config:objectthe configuration object.protocols:Array<string>The WebSocket sub-protocol. default:graphql-transport-ws.reconnect:booleanEnables the client to attempt to reconnect if the connection is interrupted. default:false.maxReconnectAttempts:numberThe maximum amount of attempts the client should perform when reconnecting. default:Infinity.serviceName:stringThe service name.connectionCallback:(params: any) : Promise<void>A callback that is executed when the client is successfully connected to the server.failedConnectionCallback:(params: any) : Promise<void>A callback that is executed when the client fails connecting to the server.failedReconnectCallback:(params: any) : Promise<void>A callback that is executed when the client fails reconnecting to the server.connectionInitPayload:anyThe connection init payload or a function that returns it.rewriteConnectionInitPayload:(connectionInit, context) => voidA function that allows to rewrite the default connection init payload.keepAlive:numberThe milliseconds value of the connection keep-alive interval. default:false
subscriptionClient.connect():voidCreates a connection to the server.subscriptionClient.close(tryReconnect, closedByUser?):voidCloses the connection to the server.unsubscribe(operationId, forceUnsubscribe):voidUnsuscribes from the topic with the providedoperationId.subscriptionClient.unsubscribeAll():voidUnsuscribes from all topics.subscriptionClient.createSubscription(query, variables, publish, context):string. Creates a subscription to a query. Returns theSubscriptionOperationId
SubscriptionClient extends EventEmitter class from node:events, and it is possibile add listeners to the following events:
SocketOpenEmits when the connection is open.readyEmits when theconnection_ackis received and it is ready to communicate.SocketErrorEmits when an error occurred.SocketCloseEmits when the socket is closed.
Examples:
const { SubscriptionClient } = require( "@mercuriusjs/subscription-client")
const { once } = require('events')
const client = new SubscriptionClient('ws://localhost:3000', {
protocols: ["graphql-transport-ws"],
reconnect: true,
maxReconnectAttempts: 10,
serviceName: "test-service",
connectionCallback: () => {
// executes callback on connection
},
failedConnectionCallback: (err) => {
// executes callback on connection error
},
failedReconnectCallback: () => {
// executes callback on reconnection error
},
connectionInitPayload: 'foo', // connection payload
rewriteConnectionInitPayload: (connectionInit, context) => {
// rewrite the connection init payload
},
keepAlive: 1000
});
client.connect()
await once(client, 'ready')
const operationId1 = client.createSubscription('query', {}, (data)=> {
// data returned from the query
})
client.unsubscribe(operationId1)
const operationId2 = client.createSubscription('query', {}, (data)=> {
// data returned from the query
})
client.unsuscribeAll()
client.close()