paho.mqtt.javascript icon indicating copy to clipboard operation
paho.mqtt.javascript copied to clipboard

Question: Can I use the paho.mqtt.javascript in NodeJS?

Open jxcheng008 opened this issue 9 years ago • 9 comments
trafficstars

Can I use the paho.mqtt.javascript in NodeJS?

jxcheng008 avatar Sep 08 '16 19:09 jxcheng008

No, you cannot. The JavaScript client is written specifically for browsers. If you have time, you can fork and make necessary changes for NodeJS, not sure how much work will that be :-)

miketran78727 avatar Sep 08 '16 20:09 miketran78727

use this https://github.com/mqttjs/MQTT.js

saulofr avatar Oct 05 '16 19:10 saulofr

kind of a hack - but if you put the patch below on top of mqttws31.js and run 'npm install ws' at least the simple examples do work in Node.JS. When looking at the dependency graph of mqtt.js I have the feeling that there is something wrong. http://npm.anvaka.com/#/view/2d/mqtt

diff --git a/mqttws31.js b/mqttws31.js
index 87b7da5..a8b7d3e 100644
--- a/mqttws31.js
+++ b/mqttws31.js
@@ -1,3 +1,22 @@
+global.WebSocket = require('ws')
+
+global.localStorage = {
+  store: {},
+  getItem: function (key) {
+    return this.store[key]
+  },
+  setItem: function (key, value) {
+    this.store[key] = value
+  },
+  removeItem: function (key) {
+    delete this.store[key]
+  }
+}
+global.window = global
+
+const Paho = {}
+module.exports = Paho
+
 /*******************************************************************************
  * Copyright (c) 2013 IBM Corp.
  *

Full example: https://github.com/pmorjan/paho-node

pmorjan avatar Oct 17 '16 17:10 pmorjan

Wow, I'd never looked at the dependencies of mqtt.js before, thats a lot! Getting this client working in node.js would be a really nice thing to do, however it would still be restricted to WebSocket connections only which isn't necessarily an issue right now. I'll put an enhancement on the backlog to investigate getting this client working in both browsers and node.js CCSDS

jpwsutton avatar Oct 18 '16 07:10 jpwsutton

@pmorjan The dep graph is misleading, because the package.json does not specify the devDependencies as devDependencies and thus jasmine-node, jshint and websocket which are only required for testing, account for the vast majority of external dependencies.

Without having it tested myself, it seems for using the lib, you just have to polyfill localStorage, which can easily achieved, by using the specified dependency node-localstorage -> https://github.com/lmaccherone/node-localstorage.

@miketran78727 Considering, that the library itself is tested using NodeJS (and jasmine) I can't see, why it would not be possible to use this library with NodeJS :)

ChristianUlbrich avatar Jul 17 '17 22:07 ChristianUlbrich

@ChristianUlbrich MQTT.js is perfectly ok. Maybe 64 external dependencies isn't that much for the features it supports but a problem of Javascript/Nodejs projects in general. For example https://www.npmjs.com/package/isarray : (used by MQTT) files: 8 Readme: 60 lines JS Code: 4 lines

node-localstorage is written in coffee-script. That could lead to a another useless discussion :-).

pmorjan avatar Jul 18 '17 18:07 pmorjan

Seems to work well. localStorage is not needed: https://github.com/eclipse/paho.mqtt.javascript/blob/master/src/paho-mqtt.js#L109

import ws from 'ws';
import { Client } from 'paho-mqtt';
global.WebSocket = ws;

const client = new Client(....

psi-4ward avatar Jan 17 '21 14:01 psi-4ward

Jap, looks like, someone fixed this 4.5 year old issue, 3 years ago. :) @jxcheng008 Would you plz. close this issue then?

ChristianUlbrich avatar Jan 18 '21 09:01 ChristianUlbrich

Is this issue fixed? Can I use this in NodeJs, if yes can someone please share an example code? Thanks! We have built an API layer application(Paho_API) using this library to connect with MQTT, which work fine in the browser when I tried to use the same(Paho_API) in a node js env I see the below error:

`ws does not work in the browser. Browser clients must use the native WebSockets. The error is kind of misleading as I'm actually running this on a NodeJs server. Appreciate your help.

Satsh24 avatar Nov 01 '21 22:11 Satsh24