1

I am trying to make Aedes works as a MQTT broker AND Websocket server. According to that doc: https://github.com/moscajs/aedes/blob/master/docs/Examples.md what i am suppose to understand. Ideally, i want the listener fired up whatever if its a websocket client or a mqtt client.

Is it possible to do something like:

server.broadcast('foo/bar', {data:''})

and all client, websockets and mqtt receive the message ? The doc is not very clear and i am very suprised that websocket-stream is used. It is very low lvl right ?

here some server side code:

    const port = 1883

    const aedes = require('aedes')({

        persistence: mongoPersistence({
            url: 'mongodb://127.0.0.1/aedes-test',
            // Optional ttl settings
            ttl: {
                packets: 300, // Number of seconds
                subscriptions: 300
            }
        }),
        authenticate: (client, username, password, callback) => {



        },
        authorizePublish: (client, packet, callback) => {

        },

        authorizeSubscribe: (client, packet, callback) => {

        }
    });

    //const server = require('net').createServer(aedes.handle);
    const httpServer = require('http').createServer()
    const ws = require('websocket-stream')
    ws.createServer({ server: httpServer }, aedes.handle)


    httpServer.listen(port, function () {
        Logger.debug('Aedes listening on port: ' + port)
        aedes.publish({ topic: 'aedes/hello', payload: "I'm broker " + aedes.id })
    });
user3178486
  • 321
  • 6
  • 17
  • I think you might be miss understanding, Websocket support is for MQTT over Websockets, not a raw Websocket connection. – hardillb Mar 11 '20 at 19:02
  • According to http://www.steves-internet-guide.com/mqtt-websockets/, MQTT server can handle client using websocket. No ? So basically, if i use the code specified here: https://github.com/moscajs/aedes/blob/master/docs/Examples.md am i able to connect from a browser to the mqtt server using ws ? – user3178486 Mar 12 '20 at 10:19
  • Yes, it can use Websockets as a transport of the MQTT protocol, not as a raw websocket – hardillb Mar 12 '20 at 10:20
  • I am sorry... but i dont get it... something like this: this._client = new Paho.MQTT.Client("localhost", 1883, "", ""); i suppose to work if Aedes is configured according the doc ? – user3178486 Mar 12 '20 at 10:54
  • Ok, it works... but now, the plain mqtt client dont want to connect. So... i am stuck at the beginning. Cant make websocket AND mqtt work at the same time on the same port. – user3178486 Mar 12 '20 at 10:58

1 Answers1

3

It should just be case of starting both servers with the same aedes object as follows:

const port = 1883
const wsPort = 8883

const aedes = require('aedes')({

    persistence: mongoPersistence({
        url: 'mongodb://127.0.0.1/aedes-test',
        // Optional ttl settings
        ttl: {
            packets: 300, // Number of seconds
            subscriptions: 300
        }
    }),
    authenticate: (client, username, password, callback) => {



    },
    authorizePublish: (client, packet, callback) => {

    },

    authorizeSubscribe: (client, packet, callback) => {

    }
});

const server = require('net').createServer(aedes.handle);
const httpServer = require('http').createServer()
const ws = require('websocket-stream')
ws.createServer({ server: httpServer }, aedes.handle)

server.listen(port, function() {
    Logger.debug('Ades MQTT listening on port: ' + port)
})

httpServer.listen(wsPort, function () {
    Logger.debug('Aedes MQTT-WS listening on port: ' + wsPort)
    aedes.publish({ topic: 'aedes/hello', payload: "I'm broker " + aedes.id })
});
hardillb
  • 54,545
  • 11
  • 67
  • 105