Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem Not Working Connection with Nodejs or Browser #1265

Closed
sergioxdev opened this issue Apr 7, 2021 · 5 comments
Closed

Problem Not Working Connection with Nodejs or Browser #1265

sergioxdev opened this issue Apr 7, 2021 · 5 comments

Comments

@sergioxdev
Copy link

sergioxdev commented Apr 7, 2021

I post the code that I am creating,
starting it from the shell with the command: node script.js

result:
lost connection! undefined
connection closed! undefined

'use strict'
var mqtt = require('mqtt')
var url = require('url')
var path = require('path')
var fs = require('fs')
var TRUSTED_CA_LIST = fs.readFileSync(path.join(__dirname, '/ca.cer'))
var endpoint = 'wss://server:port/mqtt'
var parsed = url.parse(endpoint)
var mqttOptions = {
	keepalive: 60,
	reschedulePings: true,
	protocolId: 'MQTT',
	protocolVersion: 5,
	reconnectPeriod: 1000,
	connectTimeout: 10 * 1000,
	clean: true,
	protocol: 'wss',
	clientId: 'example',
	username: 'example',
	password: 'example',
  	ca: TRUSTED_CA_LIST,
  	rejectUnauthorized: true,
	will: {
		topic: 'WillMsg',
		payload: 'Connection Closed abnormally..!',
		qos: 0,
		retain: false
	},
	debug: true,
}
var client = mqtt.connect(parsed, mqttOptions)
client.subscribe('example')

client.on('connect', function () {
  console.log('connected')
})
client.on('error', function (a) {
  console.log('error! ' + a)
})
client.on('offline', function (a) {
  console.log('lost connection! ' + a)
})
client.on('close', function (a) {
  console.log('connection closed! ' + a)
})
client.on('message', function (topic, message) {
  console.log(message.toString())
})

same code, starting it from firefox: inserted in a web page

result: it works

html
head
<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script> 

script
var TRUSTED_CA_LIST = 'example'

var endpoint = 'wss://server:port/mqtt'

var mqttOptions = {
	keepalive: 60,
	reschedulePings: true,
	protocolId: 'MQTT',
	protocolVersion: 5,
	reconnectPeriod: 1000,
	connectTimeout: 10 * 1000,
	clean: true,
	protocol: 'wss',
	clientId: 'example',
	username: 'example',
	password: 'example',
  	ca: TRUSTED_CA_LIST,
  	rejectUnauthorized: true,
	will: {
		topic: 'WillMsg',
		payload: 'Connection Closed abnormally..!',
		qos: 0,
		retain: false
	},
	debug: true,
}

var client = mqtt.connect(endpoint, mqttOptions)
client.subscribe('example')

client.on('connect', function () {
  console.log('connected')
})

client.on('error', function (a) {
  console.log('error! ' + a)
})

client.on('offline', function (a) {
  console.log('lost connection! ' + a)
})

client.on('close', function (a) {
  console.log('connection closed! ' + a)
})

client.on('message', function (topic, message) {
  console.log(message.toString())
})

/script
/head
body
/body
/html

something wrong, suggestions are welcome

@sergioxdev sergioxdev changed the title wss connection problem with nodejs WSS connection problem with Nodejs Apr 7, 2021
@sergioxdev sergioxdev changed the title WSS connection problem with Nodejs WSS connection problem with Nodejs - Not Working Apr 7, 2021
@sergioxdev sergioxdev changed the title WSS connection problem with Nodejs - Not Working Problem Not Working - WSS connection with Nodejs Apr 7, 2021
@sergioxdev
Copy link
Author

updates I kept testing:
premise:

  • certicates signed by CA authority are used
  • I have tested the services on the different ports with other clients and the brocker works fine

nodejs:

  • mqtt > OK
  • mqtts > OK
  • ws > OK
  • WSS > NO - (lost connection! undefined - connection closed! undefined)

firefox:

  • mqtt > NO - (error Javascript on -> createConnection(r))
  • mqtts > NO - (error Javascript on -> createConnection(r))
  • ws > NO - (lost connection! undefined - connection closed! undefined)
  • WSS > OK

@sergioxdev sergioxdev changed the title Problem Not Working - WSS connection with Nodejs Problem Not Working Connection with Nodejs or Browser Apr 7, 2021
@YoDaMa
Copy link
Contributor

YoDaMa commented Sep 29, 2021

@sergioxdev so I understand correctly, you are saying that with your snippet of code using WSS, it does not work in NodeJS but it does work when running the code in a Browser environment?

@YoDaMa YoDaMa self-assigned this Sep 29, 2021
@sergioxdev
Copy link
Author

@sergioxdev so I understand correctly, you are saying that with your snippet of code using WSS, it does not work in NodeJS but it does work when running the code in a Browser environment?

@YoDaMa Yes, right

@robertsLando
Copy link
Member

MQTT 5.0.0 BETA is now available! Try it out and give us feedback: npm i mqtt@beta. It may fix your issues

@robertsLando
Copy link
Member

Feel free to reopen this issue if it happens also with v5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants