Adapter: mqttws

AeroGear.Notifier.adapters. mqttws

new mqttws(clientName, settings) → {Object}

Status: Deprecated
The mqttws adapter uses MQTT over WebSockets for messaging.
Parameters:
Name Type Argument Default Description
clientName String The name used to reference this particular notifier client
settings Object <optional>
{} The settings to be passed to the adapter
Properties
Name Type Argument Default Description
autoConnect Boolean <optional>
false Automatically connect the client to the connectURL on creation. This option is ignored and a connection is automatically established if channels are provided as the connection is necessary prior to channel subscription
connectURL String <optional>
"" Defines the URL for connecting to the messaging service
onConnect Function <optional>
Callback to be executed when a connection is established if autoConnect === true
onConnectError Function <optional>
Callback to be executed when connecting to a service is unsuccessful if autoConnect === true
onMessage Function <optional>
Callback to be executed when a message is received
channels Array <optional>
[] A set of channel objects to which this client can subscribe. Each object should have a String address
Deprecated:
  • since 2.1.0 and will be removed in a future release.
    Source:
    Returns:
    The created notifier client
    Type
    Object
    Example
        // Create an empty Notifier
        var notifier = AeroGear.Notifier();
    
        // Create a channel object and the channel callback function
        var channelObject = {
            address: "org.aerogear.messaging.global"
        };
    
        // Add an mqttws client with all the settings
        notifier.add({
            name: "client1",
            settings: {
                autoConnect: true,
                connectURL: window.location.protocol + '//' + window.location.host + "/eventbus",
                onConnect: function() {
                    console.log( "connected" );
                },
                onConnectError: function() {
                    console.log( "connection error" );
                },
                onMessage: function ( message ) {
                    console.log( message.destinationName + " " + message.payloadString );
                },
                channels: [ channelObject ]
            }
        });

    Methods

    connect(options)

    Connect the client to the messaging service
    Parameters:
    Name Type Argument Default Description
    options Object <optional>
    {} Options to pass to the connect method
    Properties
    Name Type Argument Description
    url String <optional>
    The URL for the messaging service. This url will override and reset any connectURL specified when the client was created
    mqttVersion Number <optional>
    The MQTT protocol version. Should be 3 or 4.
    timeout Number <optional>
    If the connect has not succeeded within this number of seconds, it is deemed to have failed
    login String <optional>
    Authentication login name for this connection
    password String <optional>
    Authentication password for this connection
    keepAliveInterval Number <optional>
    The server disconnects this client if there is no activity for this number of seconds
    willMessage Messaging.Message <optional>
    Sent by the server when the client disconnects abnormally
    cleanSession Boolean <optional>
    If true(default) the client and server persistent state is deleted on successful connect
    useSSL Boolean <optional>
    If present and true, use an SSL Websocket connection
    onConnect Function <optional>
    Callback to be executed when a connection is established
    onConnectError Function <optional>
    Callback to be executed when connecting to a service is unsuccessful
    settings.onMessage Function <optional>
    Callback to be executed when a message is received
    Source:
    Example
        // Create an empty Notifier
        var notifier = AeroGear.Notifier();
    
        // Add an mqtt client
        notifier.add({
            name: "client1",
            settings: {
                connectURL: window.location.protocol + '//' + window.location.host + "/eventbus",
                onConnect: function() {
                    console.log( "connected" );
                },
                onConnectError: function() {
                    console.log( "connection error" );
                },
                onMessage: function ( message ) {
                    console.log( message.destinationName + " " + message.payloadString );
                }
            }
        });
    
        // Connect to the vertx messaging service
        notifier.clients.client1.connect();

    disconnect()

    Disconnect the client from the messaging service
    Source:
    Example
        // Create an empty Notifier
        var notifier = AeroGear.Notifier();
    
        // Add an mqtt client
        notifier.add({
            name: "client1",
            settings: {
                connectURL: window.location.protocol + '//' + window.location.host + "/eventbus",
                onConnect: function() {
                    console.log( "connected" );
                },
                onConnectError: function() {
                    console.log( "connection error" );
                },
                onMessage: function( message ) {
                    console.log( message.destinationName + " " + message.payloadString );
                }
            }
        });
    
        // Connect to the vertx messaging service
        notifier.clients.client1.connect();
    
        // Disconnect from the vertx messaging service
        notifier.clients.client1.disconnect();

    send(channel, message, sendOptions)

    Send a message to a particular channel
    Parameters:
    Name Type Argument Default Description
    channel String The channel to which to send the message
    message String | Object <optional>
    "" The message object to send
    sendOptions Object <optional>
    The send options to send
    Source:
    Example
        // Send an empty message to a channel
        notifier.clients.client1.send( "test.address" );
    
        // Send a "Hello" message to a channel
        notifier.clients.client1.send( "test.address", "Hello" );

    subscribe(channels, reset)

    Subscribe this client to a new channel
    Parameters:
    Name Type Argument Description
    channels Object | Array A channel object or array of channel objects to which this client can subscribe. Each object should have a String address as well as an optional subscribeOptions object which is used to control the subscription
    reset Boolean <optional>
    If true, remove all channels from the set and replace with the supplied channel(s)
    Source:
    Example
        // Create an empty Notifier
        var notifier = AeroGear.Notifier();
    
        // Create a channel object and the channel callback function
        var channelObject = {
            address: "org.aerogear.messaging.global",
            subscribeOptions: {
                qos: 2,
                onSuccess: function() {
                    console.log( 'Subscription was successful' );
                },
                onFailure: function() {
                    console.log( 'Subscription failed' );
                },
                timeout: 60
            }
        };
    
        // Add a mqtt client with autoConnect === true and no channels
        notifier.add({
            name: "client1",
            settings: {
                autoConnect: true,
                connectURL: window.location.protocol + '//' + window.location.host + "/eventbus",
                onConnect: function() {
                    console.log( "connected" );
                },
                onConnectError: function() {
                    console.log( "connection error" );
                },
                onMessage: function( message ) {
                    console.log( message.destinationName + " " + message.payloadString );
                }
            }
        });
    
        // Subscribe to a channel
        notifier.clients.client1.subscribe( channelObject );
    
        // Subscribe to multiple channels at once
        notifier.clients.client1.subscribe([
            {
                address: "newChannel",
                subscribeOptions: {...}
            },
            {
                address: "anotherChannel",
                subscribeOptions: { ... }
            }
        ]);
    
        // Subscribe to a channel, but first unsubscribe from all currently subscribed channels by adding the reset parameter
        notifier.clients.client1.subscribe({
                address: "newChannel",
                subscribeOptions: { ... }
            }, true );

    unsubscribe(channels)

    Unsubscribe this client from a channel
    Parameters:
    Name Type Description
    channels Object | Array A channel object or a set of channel objects to which this client nolonger wishes to subscribe. Each object should have a String address and an optional unsubscribeOptions object
    Source:
    Example
        // Unsubscribe from a previously subscribed channel
        notifier.clients.client1.unsubscribe(
            {
                address: "org.aerogear.messaging.global",
                unsubscribeOptions: {
                    onSuccess: function() {
                        console.log( 'Unusubscribe was successful' );
                    },
                    onFailure: function() {
                        console.log( 'Unsubscribe failed' );
                    },
                    timeout: 20
                }
            }
        );
    
        // Unsubscribe from multiple channels
        notifier.clients.client1.unsubscribe([
            {
                address: "newChannel",
                unsubscribeOptions: { ... }
            },
            {
                address: "anotherChannel"
            }
        ]);