Can't find variable: Buffer (React Native App)

I am trying to use losant-mqtt JS from within a React Native mobile application. For this, I used the repository available at GitHub: https://github.com/Losant/losant-mqtt-js

Since React Native does not run in a node.js environment, I had to install dependent modules as used by losant. Specifically I did npm install util events mongodb-extended-json buffer.

When I deploy my app, I get this error:
Can't find variable: Buffer

The error appears from file bson/lib/bson/bson.js:30

At the start of file bson/lib/bson/binary.js, the following code is present:

// Test if we're in Node via presence of "global" not absence of "window"
// to support hybrid environments like Electron
if(typeof global !== 'undefined') {
  var Buffer = require('buffer').Buffer; // TODO just use global Buffer
}

Has anyone tried using Losant-MQTT with React Native? Thanks.

@Arvind_Padmanabhan So there is a couple of things here.

When you did: npm install util events mongodb-extended-json buffer, you installed the buffer npm module. mongodb-extended-json uses the Native node.js buffer core module. They are very different.

Secondly, depending on your use case, you can just use our API or MQTT directly. Check out:

It doesn’t look like it has any dependencies that would conflict with React Native.
Also check out our MQTT docs:

Let me know if that works. This is a cool use case. :hugs:

1 Like

Thanks for the quick response. I solved the buffer problem but encountered other issues. For now, I am putting this hold.

Yesterday I tried losant-rest-js. I managed to integrate this with my React Native Android app. It went smoothly.

Your other suggestion of using MQTT JS directly, I think I will try this.

So far, I have found Losant easy to learn and use.

1 Like

Sweet! I’m happy to hear that. If have anymore questions, let us know!