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

Connect to unix socket #1040

Closed
ralight opened this issue Feb 6, 2020 · 16 comments · Fixed by #1852
Closed

Connect to unix socket #1040

ralight opened this issue Feb 6, 2020 · 16 comments · Fixed by #1852

Comments

@ralight
Copy link
Contributor

ralight commented Feb 6, 2020

Would you consider support for connecting to unix sockets? The use case is a local application wanting to connect to a local MQTT broker without having to create a network.

It looks like lib/connect/unix.js would be very similar to tcp.js, but with this sort of construct instead:

    unixpath = '/var/run/mqtt.socket'

    return net.createConnection(unixpath)
@mcollina
Copy link
Member

mcollina commented Feb 6, 2020 via email

@ralight
Copy link
Contributor Author

ralight commented Feb 11, 2020

Thanks, I'll take a look.

@natcl
Copy link

natcl commented Mar 31, 2022

Any updates on this ? Would be interested in this feature too !

@ralight
Copy link
Contributor Author

ralight commented Mar 31, 2022

I hadn't done any more because of this: #1094 , but that has been closed now for some reason. It wasn't clear what to use as the URL prefix, otherwise I suspect it may have got further.

I've been using my own modified version just fine, I'd be happy for anyone to take this over.

@RuStrannik
Copy link

RuStrannik commented Apr 6, 2022

I suggest using unix:// prefix, which also used in nginx configs (those are really widespread and well known). Native mosquitto clients also use option --unix to specify path to unix socket.

I am really confused why it has not been accepted yet. This feature is clearly useful for security, doesn't break anything, and there is not that much to review! Literally 1 line of code and 1 file with 6 lines of really simple and straight-forward code added. Is project dead / in hibernation or what?

@github-actions
Copy link

This is an automated message to let you know that this issue has
gone 365 days without any activity. In order to ensure that we work
on issues that still matter, this issue will be closed in 14 days.

If this issue is still important, you can simply comment with a
"bump" to keep it open.

Thank you for your contribution.

@github-actions github-actions bot added the stale label May 18, 2023
@natcl
Copy link

natcl commented May 18, 2023

We're still interested in this!

@github-actions github-actions bot removed the stale label May 19, 2023
@nik-sie
Copy link

nik-sie commented Feb 5, 2024

It's actually possible to connect to a unix socket right now.
Check this out:

'use strict';
const mqtt = require('mqtt');
const cfg = { path: "/run/mosquitto/mosquitto.sock", username: "user", password: "pass" };
const client = mqtt.connect(cfg);
client.on('error', (err) => { if (err) { console.error(err); }; });
client.on('connect', (conn_evt) => {
    console.log("mqtt: connected. Subscribing...");
    client.subscribe(['#'], { nl: true }, (err, granted) => { console.log("granted:", granted); });
});
client.on('message', (path, data, packet) => { console.log("mqtt_msg_in: ['%s']:(%d) '%s'", path, data.length, data.toString()); });

@robertsLando
Copy link
Member

I dunno how that code could work @nik-sie, the PR adding unix socket support has never been merged. Maybe the default tcp handler could work as it support sockets too

@nik-sie
Copy link

nik-sie commented Feb 6, 2024

@robertsLando, If you really want to use unix sockets, just try it, it works, I've been using it for a long time.
It works, since MqttClient uses node's native net.createConnection() call under the hood, which accepts path option (if given) to create connection to an IPC.
The PR would only add convenience of using URLs, that's it.

@rockey2020
Copy link

rockey2020 commented Apr 24, 2024

no working at 5.5.3

 const mqttOptions: IClientOptions = {
    path: createMQTTClientProps.remoteConnection.address,
  };

  console.log(mqttOptions);

  const mqttClient: MqttClient = await mqtt.connectAsync(
    createMQTTClientProps.remoteConnection.address,
  );

image

when i replace unix string ,stil no working

@nik-sie
Copy link

nik-sie commented Apr 25, 2024

@rockey2020, there should NOT be any prefixes to the path, remove the unix:// part.
Also, make sure your mqtt client actually has read+write access to the socket file.

@nik-sie
Copy link

nik-sie commented Apr 25, 2024

I did a quick test and, apparently, compiled ts -> js output indeed changed drastically, so old simple method doesn't work anymore.
Here is how it works now:

'use strict';

const net = require('node:net');
const mqtt = require('mqtt');

function streamBuilder() { return net.createConnection({ path: "/run/mosquitto/mosquitto.sock" }); };
function msgRecv(path, data, packet) { console.log("mqtt_msg_in: ['%s']:(%d) '%s'", path, data.length, data.toString()); };

const client = new mqtt.Client(streamBuilder, { username: "user", password: "pass" });
client.on('error', (err) => { if (!err) { return; }; console.error(err); client.end(); });
client.on('connect', (conn_evt) => {
    console.log("mqtt: connected. Subscribing...");
    client.subscribe(['#'], { nl: true }, (err, granted) => { console.log("granted:", granted); });
});
client.on('message', msgRecv);

@robertsLando
Copy link
Member

I think the problem is the path argument is not passed down correctly to the createConnection, does someone wants to submit a PR for that?

@drdaeman
Copy link

drdaeman commented May 5, 2024

May I suggest reopening this (or should I create a new issue?) for URI-based support, e.g. mqtt:///run/mosquitto/mosquitto.socket, mqtt+unix:///run/mosquitto/mosquitto.socket, or something similar?

The idea is that it would allow various projects (I'm thinking about Zigbee2MQTT, which currently doesn't seem to have a way to specify path in their configuration file) that use this library to automagically start supporting connecting to UNIX sockets without any code changes besides, of course, bumping their mqtt.js dependency version.

Thanks!

@robertsLando
Copy link
Member

@drdaeman want to submit a PR?

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