async connectNewClient()

in packages/aws-appsync-subscription-link/src/subscription-handshake-link.ts [159:190]


    async connectNewClient(connectionInfo: MqttConnectionInfo, observer: ZenObservable.Observer<FetchResult>, selectionNames: string[]) {
        const { client: clientId, url, topics } = connectionInfo;
        const client: any = new Paho.Client(url, clientId);

        client.trace = mqttLogger.bind(null, clientId);
        client.onConnectionLost = ({ errorCode, ...args }) => {
            if (errorCode !== 0) {
                topics.forEach(t => {
                    if (this.topicObservers.has(t)) {
                        this.topicObservers.get(t).forEach(observer => observer.error({ ...args, permanent: true }));
                    }
                });
            }

            topics.forEach(t => this.topicObservers.delete(t));
        };

        (client as any).onMessageArrived = ({ destinationName, payloadString }) => this.onMessage(destinationName, payloadString, selectionNames);

        await new Promise((resolve, reject) => {
            client.connect({
                useSSL: url.indexOf('wss://') === 0,
                mqttVersion: 3,
                onSuccess: () => resolve(client),
                onFailure: reject,
            });
        });

        await this.subscribeToTopics(client, topics, observer);

        return client;
    }