UNPKG

@nymphjs/pubsub

Version:

Nymph.js - PubSub Server

105 lines (104 loc) 2.79 kB
import { Nymph } from '@nymphjs/nymph'; import ws from 'websocket'; import { Config } from './conf/index.js'; import type { QuerySubscriptionData } from './PubSub.types.js'; /** * A publish/subscribe server for Nymph. * * Written by Hunter Perrin for SciActive. * * @author Hunter Perrin <hperrin@gmail.com> * @copyright SciActive Inc * @see http://nymph.io/ */ export default class PubSub { /** * The Nymph instance. */ nymph: Nymph; /** * The PubSub config. */ config: Config; /** * The WebSocket server. */ server: ws.server; private sessions; protected querySubs: { [etype: string]: { [query: string]: Map<ws.connection, QuerySubscriptionData>; }; }; protected uidSubs: { [uidName: string]: Map<ws.connection, { count: boolean; }>; }; protected static transactionPublishes: { nymph: Nymph; payload: string; config: Config; }[]; static initPublisher(config: Partial<Config>, nymph: Nymph): () => void; private static publish; private static isOrIsDescendant; private static publishTransactionPublishes; private static removeTransactionPublishes; /** * Initialize Nymph PubSub. * * @param config The PubSub configuration. */ constructor(config: Partial<Config>, nymph: Nymph, server: ws.server); close(): void; handleRequest(request: ws.request): void; /** * Handle a message from a client. */ onMessage(from: ws.connection, msg: ws.Message): Promise<void>; /** * Clean up after users who leave. */ onClose(conn: ws.connection, description: string): void; onError(conn: ws.connection, e: Error): void; /** * Handle an authentication from a client. */ private handleAuthentication; /** * Handle a subscribe or unsubscribe from a client. */ private handleSubscription; /** * Handle a subscribe or unsubscribe for a query from a client. */ private handleSubscriptionQuery; /** * Handle a subscribe or unsubscribe for a UID from a client. */ private handleSubscriptionUid; /** * Handle a publish from a client. */ private handlePublish; /** * Handle an entity publish from a client. */ private handlePublishEntity; private updateClient; /** * Handle a UID publish from a client. */ private handlePublishUid; /** * Relay publish data to other servers. */ private relay; private findQRefQueries; /** * This translates qref selectors into ref selectors using the "current" GUID * list in the existing subscriptions. */ private translateQRefSelectors; }