How to use the rhea.listen function in rhea

To help you get started, we’ve selected a few rhea examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github amqp / rhea / examples / tls / tls_server.js View on Github external
var fs = require('fs');
var path = require('path');
var args = require('../options.js').options({
    'p': { alias: 'port', default: 5671, describe: 'port to listen on'}
}).help('help').argv;

container.on('connection_open', function (context) {
    var cert = context.connection.get_peer_certificate();
    var cn;
    if (cert && cert.subject) cn = cert.subject.CN;
    var tls = context.connection.get_tls_socket();
    var servername;
    if (tls && tls.servername) servername = tls.servername;
    console.log('Connected: ' + cn + ((tls && tls.servername) ? ' [' + tls.servername + ']' : ''));
});
var listener = container.listen({port:args.port, transport:'tls',
    //enable_sasl_external:true,
    key: fs.readFileSync(path.resolve(__dirname, 'server-key.pem')),
    cert: fs.readFileSync(path.resolve(__dirname,'server-cert.pem')),

    // to require client authentication:
    requestCert: true,
    rejectUnauthorized: true,
    ca: [ fs.readFileSync(path.resolve(__dirname,'ca-cert.pem')) ]
});
listener.on('clientError', function (error, socket) {
    console.log(error);
});
github amqp / rhea / examples / direct_recv.js View on Github external
* distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var container = require('rhea');

var args = require('./options.js').options({
    'm': { alias: 'messages', default: 100, describe: 'number of messages to expect'},
    'p': { alias: 'port', default: 8888, describe: 'port to connect to'}
}).help('help').argv;

var received = 0;
var expected = args.messages;

var server = container.listen({ port: args.port });

container.on('message', function (context) {
    if (context.message.id && context.message.id < received) {
        // ignore duplicate message
        return;
    }
    if (expected === 0 || received < expected) {
        console.log(context.message.body);
        if (++received === expected) {
            context.receiver.detach();
            context.connection.close();
            server.close();
        }
    }
});
github mthirion / microsaga / cards / src / main / amq / node_modules / rhea / examples / sasl / sasl_plain_server.js View on Github external
}).help('help').argv;

/**
 * To authenticate using PLAIN and a simple username and password
 * combination, the application provides a callback function for
 * authenticating a connecting user given their specified username and
 * password. (The test used here - namely that the password is always
 * the username in reverse - is of course NOT recommended in practice!
 * :-)
 */
function authenticate(username, password) {
    console.log('Authenticating as ' + username);
    return username.split("").reverse().join("") === password;
}
container.sasl_server_mechanisms.enable_plain(authenticate);
var server = container.listen({'port':args.port});
container.on('connection_open', function (context) {
    context.connection.local.open.properties = {'foo':'abc'};
    console.log('Connected!');
});
github amqp / rhea / examples / sasl / sasl_anonymous_server.js View on Github external
*     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var container = require('rhea');
var args = require('../options.js').options({
    'h': { alias: 'host', default: 'localhost', describe: 'dns or ip name of server where you want to connect'},
    'p': { alias: 'port', default: 5672, describe: 'port to listen on'}
}).help('help').argv;

container.sasl_server_mechanisms.enable_anonymous();
var server = container.listen({ port: args.port, host: args.host });
container.on('connection_open', function (context) {
    console.log('Connected!');
});
github mthirion / microsaga / cards / src / main / amq / node_modules / rhea / examples / sasl / sasl_anonymous_server.js View on Github external
*
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var container = require('rhea');
var args = require('../options.js').options({
      'p': { alias: 'port', default: 5672, describe: 'port to listen on'}
    }).help('help').argv;

container.sasl_server_mechanisms.enable_anonymous();
var server = container.listen({'port':args.port});
container.on('connection_open', function (context) {
    console.log('Connected!');
});
github mthirion / microsaga / cards / src / main / amq / node_modules / rhea / examples / direct_helloworld.js View on Github external
* distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var container = require('rhea');
container.on('message', function (context) {
    console.log(context.message.body);
});
container.on('accepted', function (context) {
    context.connection.close();
});
container.once('sendable', function (context) {
    context.sender.send({body:'Hello World!'});
});
var server = container.listen({'port':8888});
server.once('listening', function () {
    container.connect({'port':8888}).open_sender('examples');
});
server.once('connection', function () {
    server.close();
});
github amqp / rhea / examples / direct_send.js View on Github external
*     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var container = require('rhea');

var args = require('./options.js').options({
    'm': { alias: 'messages', default: 100, describe: 'number of messages to send'},
    'p': { alias: 'port', default: 8888, describe: 'port to connect to'}
}).help('help').argv;

var server = container.listen({'port':args.port});

var confirmed = 0, sent = 0;
var total = args.messages;

container.on('sendable', function (context) {
    while (context.sender.sendable() && sent < total) {
        sent++;
        console.log('sent ' + sent);
        context.sender.send({message_id:sent, body:{'sequence':sent}});
    }
    if (sent === total) {
        context.sender.set_drained(sent === total);
    }
});
container.on('accepted', function (context) {
    if (++confirmed === total) {
github EnMasseProject / enmasse / none-authservice / none-authservice.js View on Github external
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var container = require('rhea');
var fs = require('fs');
var path = require('path');
var cert_dir = (process.env.CERT_DIR !== undefined) ? process.env.CERT_DIR : "/opt/none-authservice/cert";

function authenticate(username, password) {
    console.log('Authenticating as ' + username);
    return true;
}
container.sasl_server_mechanisms.enable_plain(authenticate);
container.sasl_server_mechanisms.enable_anonymous();
var server = container.listen({
    port: process.env.LISTENPORT,
    require_sasl: true,
    transport: 'tls',
    key: fs.readFileSync(path.resolve(cert_dir, 'tls.key')),
    cert: fs.readFileSync(path.resolve(cert_dir, 'tls.crt'))
});

console.log('Listening on port ' + process.env.LISTENPORT);
container.on('connection_open', function (context) {
    var authenticatedIdentity = { 'sub' : context.connection.sasl_transport.username || 'anonymous' };
    var groups = [ "manage" ];
    var properties = context.connection.local.open.properties || {};
    properties["authenticated-identity"] = authenticatedIdentity;
    properties["groups"] = groups;
    context.connection.local.open.properties = properties;
    context.connection.close();
github mthirion / microsaga / cards / src / main / amq / node_modules / rhea / examples / direct_server.js View on Github external
*     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var container = require('rhea');

var args = require('./options.js').options({
      'p': { alias: 'port', default: 5672, describe: 'port to connect to'}
    }).help('help').argv;

//container.sasl_server_mechanisms.enable_anonymous();
var server = container.listen({'port':args.port});

container.on('receiver_open', function (context) {
    context.receiver.set_target({address:context.receiver.remote.attach.target.address});
});

container.on('sender_open', function (context) {
    if (context.sender.source.dynamic) {
        var id = container.generate_uuid();
        context.sender.set_source({address:id});
    }
});

function match_source_address(link, address) {
    return link && link.local && link.local.attach && link.local.attach.source
        && link.local.attach.source.address === address;
}
github amqp / rhea / examples / reconnect / echo.js View on Github external
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var container = require('rhea');

var args = require('../options.js').options({
    'm': { alias: 'messages', default: 0, describe: 'number of messages to expect'},
    'p': { alias: 'port', default: 8888, describe: 'port to connect to'}
}).help('help').argv;

var received = 0;
var expected = args.messages;
var listeners = {};

var server = container.listen({ port: args.port });

function subscribe(name, sender) {
    listeners[name] = sender;
}

function unsubscribe(name) {
    delete listeners[name];
    if (Object.getOwnPropertyNames(listeners).length === 0) {
        server.close();
    }
}

container.on('sender_open', function (context) {
    subscribe(context.connection.container_id, context.sender);
});
container.on('sender_close', function (context) {