MQTT.js icon indicating copy to clipboard operation
MQTT.js copied to clipboard

Connect to unix socket

Open ralight opened this issue 5 years ago • 10 comments

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)

ralight avatar Feb 06 '20 12:02 ralight

Definitely! I have no bandwidth to work on this, but I’ll be happy to review a PR Il 6 feb 2020, 13:03 +0100, Roger Light [email protected], ha scritto:

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) — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

mcollina avatar Feb 06 '20 12:02 mcollina

Thanks, I'll take a look.

ralight avatar Feb 11 '20 10:02 ralight

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

natcl avatar Mar 31 '22 14:03 natcl

I hadn't done any more because of this: https://github.com/mqttjs/MQTT.js/pull/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.

ralight avatar Mar 31 '22 17:03 ralight

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?

RuStrannik avatar Apr 06 '22 15:04 RuStrannik

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[bot] avatar May 18 '23 01:05 github-actions[bot]

We're still interested in this!

natcl avatar May 18 '23 11:05 natcl

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()); });

nik-sie avatar Feb 05 '24 18:02 nik-sie

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

robertsLando avatar Feb 06 '24 08:02 robertsLando

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

nik-sie avatar Feb 06 '24 14:02 nik-sie