eclipse / paho.mqtt.javascript

paho.mqtt.javascript
Other
1.15k stars 468 forks source link

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

Open jxcheng008 opened 8 years ago

jxcheng008 commented 8 years ago

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

miketran78727 commented 8 years ago

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 :-)

saulofr commented 7 years ago

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

pmorjan commented 7 years ago

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

jpwsutton commented 7 years ago

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

ChristianUlbrich commented 7 years ago

@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 :)

pmorjan commented 7 years ago

@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 :-).

psi-4ward commented 3 years ago

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(....
ChristianUlbrich commented 3 years ago

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

Satsh24 commented 2 years ago

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.