websocketmqttaedes

Mqtt and Websocket at the same time with Aedes


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

Solution

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