copyright | lastupdated | ||
---|---|---|---|
|
2017-02-13 |
{:new_window: target="_blank"} {:shortdesc: .shortdesc} {:screen: .screen} {:codeblock: .codeblock} {:pre: .pre}
{: #mql_connect}
To connect an app to the service, the app must use the user
,
password
, and mqlight_lookup_url
details from the VCAP_SERVICES environment variable. Use the following guidance for your chosen language:
For Java
If you specify ‘null’ as the endpointService parameter of the create() call, this instructs the
client to read the user
, password
and,
mqlight_lookup_url
details from VCAP_SERVICES:
NonBlockingClient.create(null, new NonBlockingClientAdapter() {
public void onStarted(NonBlockingClient client, Void context) {
client.send("my/topic", "Hello World!", null);
}
}, null);
{:codeblock}
For Node.js
Retrieve the user
, password
, and
mqlight_lookup_url
details from VCAP_SERVICES and use them to create the client as
follows:
var services = JSON.parse(process.env.VCAP_SERVICES);
mqlightService = services['messagehub'][0];
opts.service = mqlightService.credentials.mqlight_lookup_url;
opts.user = mqlightService.credentials.user;
opts.password = mqlightService.credentials.password;
var mqlightClient = mqlight.createClient(opts, function(err) {
...
{:codeblock}
For Ruby
Retrieve the user
, password
, and
mqlight_lookup_url
details from VCAP_SERVICES and use them to create the client as
follows:
vcap_services = JSON.parse(ENV['VCAP_SERVICES'])
conn_details = vcap_services['messagehub']
credentials = conn_details.first['credentials']
service = credentials['mqlight_lookup_url']
opts[:user] = credentials['user']
opts[:password] = credentials['password']
set :client, Mqlight::BlockingClient.new(service, opts)
...
{:codeblock}
For Python
Retrieve the user
, password
, and
mqlight_lookup_url
details from VCAP_SERVICES and use them to create the client as
follows:
vcap_services = json.loads(os.environ.get('VCAP_SERVICES'))
conn_details = vcap_services['messagehub'][0]
service = str(conn_details['credentials']['mqlight_lookup_url'])
security_options = {
'user': str(conn_details['credentials']['user']),
'password': str(conn_details['credentials']['password'])
}
client = mqlight.Client(service=service,
security_options=security_options,
on_started=on_started)
{:codeblock}
For more information about the {{site.data.keyword.mql}} APIs, see: {{site.data.keyword.mql}} developerWorks® site {:new_window}.