// Module included in the following assemblies:
//
// assembly-connecting-applications.adoc

[id='ref-javascript-example-{context}']
= {ClientAmqpJavaScriptName} example

You can use the following {ClientAmqpJavaScriptName} example to connect your application to {ProductName}. This example assumes you have created an address of type `queue` named `myqueue`.

[source,javascript,options="nowrap",subs="+quotes,attributes"]
----
var container = require('rhea');
container.on('connection_open', function (context) {
    context.connection.open_receiver('myqueue');
    context.connection.open_sender('myqueue');
});
container.on('message', function (context) {
    console.log(context.message.body);
    context.connection.close();
});
container.on('sendable', function (context) {
    context.sender.send({body:'Hello World!'});
    context.sender.detach();
});
container.connect({username: '_username_', password: '_password_', port:443, host:'_messaging-route-hostname_', transport:'tls', rejectUnauthorized:false});
----

== {ClientAmqpJavaScriptName} example using WebSockets

[source,javascript,options="nowrap",subs="+quotes,attributes"]
----
var container = require('rhea');
var WebSocket = require('ws');

container.on('connection_open', function (context) {
    context.connection.open_receiver('myqueue');
    context.connection.open_sender('myqueue');
});
container.on('message', function (context) {
    console.log(context.message.body);
    context.connection.close();
});
container.on('sendable', function (context) {
    context.sender.send({body:'Hello World!'});
    context.sender.detach();
});

var ws = container.websocket_connect(WebSocket);
container.connect({username: '_username_', password: '_password_', connection_details: ws("wss://_messaging-route-hostname_:443", ["binary"], {rejectUnauthorized: false})});
----

