* Connects to the IoT endpoint over websockets and listens for lambda events. * The method subscribes to all function invocation topics and listens for incoming events. * When an event is received, the method invokes the corresponding function locally, waits for the result, * and publishes
()
| 850 | * @returns {Promise<void>} This is a long-running method, so it does not return a value. |
| 851 | */ |
| 852 | async connect() { |
| 853 | const mainProgress = progress.get('main') |
| 854 | logger.debug('Connecting to IoT endpoint') |
| 855 | |
| 856 | const endpoint = await this.getIotEndpoint() |
| 857 | |
| 858 | const { |
| 859 | accessKeyId, |
| 860 | secretAccessKey: secretKey, |
| 861 | sessionToken, |
| 862 | } = await this.provider.getCredentials() |
| 863 | |
| 864 | const device = new iot.device({ |
| 865 | protocol: 'wss', |
| 866 | host: endpoint, |
| 867 | accessKeyId, |
| 868 | secretKey, |
| 869 | sessionToken, |
| 870 | autoResubscribe: true, |
| 871 | offlineQueueing: true, |
| 872 | baseReconnectTimeMs: 1000, |
| 873 | maximumReconnectTimeMs: 1000, |
| 874 | minimumConnectionTimeMs: 1000, |
| 875 | keepalive: 1, |
| 876 | }) |
| 877 | |
| 878 | device.on('error', (e) => { |
| 879 | logger.debug('IoT connection error', e) |
| 880 | }) |
| 881 | |
| 882 | device.on('offline', (e) => { |
| 883 | mainProgress.notice('Reconnecting') |
| 884 | }) |
| 885 | |
| 886 | device.on('connect', (e) => { |
| 887 | if (!this.heartbeatInterval) { |
| 888 | this.heartbeatInterval = setInterval(() => { |
| 889 | device.publish( |
| 890 | this.getTopicId('_heartbeat'), |
| 891 | JSON.stringify({ connected: true }), |
| 892 | { |
| 893 | qos: 1, |
| 894 | }, |
| 895 | ) |
| 896 | }, 1000) |
| 897 | } |
| 898 | |
| 899 | logger.success(`Connected (Ctrl+C to cancel)`) |
| 900 | mainProgress.remove() |
| 901 | }) |
| 902 | |
| 903 | // Each function has a seperate topic we need to subscribe to |
| 904 | const functionNames = this.serverless.service.getAllFunctions() |
| 905 | |
| 906 | for (const functionName of functionNames) { |
| 907 | device.subscribe(this.getTopicId(`${functionName}/request`), { |
| 908 | qos: 1, |
| 909 | }) |
no test coverage detected