Trouble importing MQTT Library in Trigger Custom Function

Hello,

I’m fairly new to working with Typescript/NodeJS. I was able to follow tutorials to get a custom function built for a trigger. I have the handler.ts file and the rest of the file structure saved locally. Prior to today, the “8base deploy” command successfully deployed my trigger.

However, I am running into trouble importing this MQTT library. Following the instructions from the library’s author, I installed the library through npm with this command: npm install mqtt --save . After, I ran npm install mqtt -g. I noticed that the node_modules folder in my local 8base workspace now includes the mqtt library, so I assume this part was successful.

I added import * as mqtt from "mqtt"; alongside the other imports in my trigger’s handler.ts file. After this, I tried “8base deploy” and got this long error:

8base deploy
⠋ deploying…
Step: complete_error
{“response”:{“errors”:[{“message”:“Deployment error.”,“details”:{“compile”:"Compilation failed:\nError: "node_modules/mqtt-packet/types/index.d.ts" (1,23): Cannot find namespace ‘NodeJS’.\nError: "node_modules/mqtt-packet/types/index.d.ts" (2,25): Cannot find namespace ‘NodeJS’.\nError: "node_modules/mqtt-packet/types/index.d.ts" (35,26): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_modules/mqtt-packet/types/index.d.ts" (49,14): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_modules/mqtt-packet/types/index.d.ts" (52,14): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_modules/mqtt-packet/types/index.d.ts" (61,25): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_modules/mqtt-packet/types/index.d.ts" (74,26): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_modules/mqtt-packet/types/index.d.ts" (84,21): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_modules/mqtt-packet/types/index.d.ts" (90,23): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_modules/mqtt-packet/types/index.d.ts" (119,26): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_modules/mqtt-packet/types/index.d.ts" (244,17): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_modules/mqtt-packet/types/index.d.ts" (249,66): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_modules/mqtt/types/lib/client-options.d.ts" (4,35): Cannot find module ‘http’ or its corresponding type declarations.\nError: "node_modules/mqtt/types/lib/client-options.d.ts" (84,14): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_modules/mqtt/types/lib/client-options.d.ts" (102,25): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_modules/mqtt/types/lib/client-options.d.ts" (116,26): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_modules/mqtt/types/lib/client-options.d.ts" (124,29): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_modules/mqtt/types/lib/client-options.d.ts" (124,38): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_modules/mqtt/types/lib/client-options.d.ts" (128,30): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_modules/mqtt/types/lib/client-options.d.ts" (128,39): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_modules/mqtt/types/lib/client-options.d.ts" (132,28): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.\nError: "node_moderror: Compilation failed:
Error: “node_modules/mqtt-packet/types/index.d.ts” (1,23): Cannot find namespace ‘NodeJS’.
Error: “node_modules/mqtt-packet/types/index.d.ts” (2,25): Cannot find namespace ‘NodeJS’.
Error: “node_modules/mqtt-packet/types/index.d.ts” (35,26): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt-packet/types/index.d.ts” (49,14): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt-packet/types/index.d.ts” (52,14): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt-packet/types/index.d.ts” (61,25): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt-packet/types/index.d.ts” (74,26): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt-packet/types/index.d.ts” (84,21): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt-packet/types/index.d.ts” (90,23): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt-packet/types/index.d.ts” (119,26): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt-packet/types/index.d.ts” (244,17): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt-packet/types/index.d.ts” (249,66): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt/types/lib/client-options.d.ts” (4,35): Cannot find module ‘http’ or its corresponding type declarations.
Error: “node_modules/mqtt/types/lib/client-options.d.ts” (84,14): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt/types/lib/client-options.d.ts” (102,25): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt/types/lib/client-options.d.ts” (116,26): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt/types/lib/client-options.d.ts” (124,29): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt/types/lib/client-options.d.ts” (124,38): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt/types/lib/client-options.d.ts” (128,30): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt/types/lib/client-options.d.ts” (128,39): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt/types/lib/client-options.d.ts” (132,28): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt/types/lib/client-options.d.ts” (132,37): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt/types/lib/client-options.d.ts” (137,30): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt/types/lib/client-options.d.ts” (137,56): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt/types/lib/client-options.d.ts” (160,23): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt/types/lib/client.d.ts” (1,23): Cannot find type definition file for ‘node’.
Error: “node_modules/mqtt/types/lib/client.d.ts” (3,25): Cannot find module ‘events’ or its corresponding type declarations.
Error: “node_modules/mqtt/types/lib/client.d.ts” (72,66): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt/types/lib/client.d.ts” (144,52): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
Error: “node_modules/mqtt/types/lib/client.d.ts” (146,52): Cannot find name ‘Buffer’. Do you need to install type definitions for node? Try npm i --save-dev @types/node.

It suggests running npm i --save-dev @types/node, so I tried that, but it didn’t help. If it makes any difference, my node version is v16.15.1. I’m running this from a Linux Mint machine.

Buried in that message, there’s a reference to Cannot find namespace 'NodeJS'. This makes me think I’ve messed something up with my node configuration, but I am experienced enough with Node to know what that would be.

Hey @jobrien9 - I don’t think that MQTT will work when deployed to a serverless environment. MDTT is going to rely on having a running server that allows the WebSocket connections to be established and held. Serverless functions run and die based on a single invocation.

If you need a websocket facility, try using Subscriptions on the graphQL API. Here’s a video about using subscriptions in building a chat room:

I was able to resolve this by reinstalling node and typescript. Apparently, I had messed up my installation somehow. In order to get the MQTT working with the trigger, I used the async-mqtt library (import * as asyncMQTT from “async-mqtt”; )

As Sebastian noted, I’d likely need something more permanent than a serverless function in order to receive MQTT notifications. However, I was able to publish to an MQTT topic using awaits and promises in my code. Here’s the relevant part of my handler.ts:

 async function promiseFunction() {
    return await new Promise<string>(resolve => {
      var client  = asyncMQTT.connect({hostname: Credentials.url, clientId:'8BaseTrigger', username: Credentials.username, password: Credentials.password,
        protocol: 'mqtts', port: 8883});

        client.on('connect', async function () {
          console.log('client connect!');
          try {            
            await client.publish(topicName + String(machineId), messageAsJson);
            console.log("finished publish");
		        await client.end();
            console.log("finished end");
          } catch(error) {
            console.log("error");
            console.log(error);
          }

          resolve(messageAsJson);
        });
   });
  }


  var result = await promiseFunction();
1 Like

Glad you got it working! Always exciting to see a creative usecase :+1: