Chapter 6. Network connections

6.1. Creating outgoing connections

To connect to a remote server, pass connection options containing the host and port to the container.connect() method.

Example: Creating outgoing connections

container.on("connection_open", function (event) {
    console.log("Connection " + event.connection + " is open");
});

var opts = {
    host: "example.com",
    port: 5672
};

container.connect(opts);

The default host is localhost. The default port is 5672.

For information about creating secure connections, Chapter 7, Security.

6.2. Configuring reconnect

Reconnect allows a client to recover from lost connections. It is used to ensure that the components in a distributed system reestablish communication after temporary network or component failures.

AMQ JavaScript enables reconnect by default. If a connection attempt fails, the client will try again after a brief delay. The delay increases exponentially for each new attempt, up to a default maximum of 60 seconds.

To disable reconnect, set the reconnect connection option to false.

Example: Disabling reconnect

var opts = {
    host: "example.com",
    reconnect: false
};

container.connect(opts);

To control the delays between connection attempts, set the initial_reconnect_delay and max_reconnect_delay connection options. Delay options are specified in milliseconds.

To limit the number of reconnect attempts, set the reconnect_limit option.

Example: Configuring reconnect

var opts = {
    host: "example.com",
    initial_reconnect_delay: 100,
    max_reconnect_delay: 60 * 1000,
    reconnect_limit: 10
};

container.connect(opts);

6.3. Configuring failover

AMQ JavaScript allows you to configure alternate connection endpoints programatically.

To specify multiple connection endpoints, define a function that returns new connection options and pass the function in the connection_details option. The function is called once for each connection attempt.

Example: Configuring failover

var hosts = ["alpha.example.com", "beta.example.com"];
var index = -1;

function failover_fn() {
    index += 1;

    if (index == hosts.length) index = 0;

    return {host: hosts[index].hostname};
};

var opts = {
    host: "example.com",
    connection_details: failover_fn
}

container.connect(opts);

This example implements repeating round-robin failover for a list of hosts. You can use this interface to implement your own failover behavior.

6.4. Accepting incoming connections

AMQ JavaScript can accept inbound network connections, enabling you to build custom messaging servers.

To start listening for connections, use the container.listen() method with options containing the local host address and port to listen on.

Example: Accepting incoming connections

container.on("connection_open", function (event) {
    console.log("New incoming connection " + event.connection);
});

var opts = {
    host: "0.0.0.0",
    port: 5672
};

container.listen(opts);

The special IP address 0.0.0.0 listens on all available IPv4 interfaces. To listen on all IPv6 interfaces, use [::0].

For more information, see the server receive.js example.