Constructor
new Client(config)
Parameters:
Name | Type | Description |
---|---|---|
config |
Config | Object containing the configuration settings for the client |
Example
var dxl = require('@opendxl/dxl-client')
var fs = require('fs')
var config = new dxl.Config(
fs.readFileSync('c:\\certs\\brokercerts.crt'),
fs.readFileSync('c:\\certs\\client.crt'),
fs.readFileSync('c:\\certs\\client.key'),
[dxl.Broker.parse('ssl://192.168.99.100')])
var client = new dxl.Client(config)
client.connect()
Members
config :Config
The Config instance that was specified when the client was constructed.
Type:
connected
Properties:
Type | Description |
---|---|
Boolean | Whether or not the client is currently connected to the DXL fabric. |
currentBroker
Properties:
Type | Description |
---|---|
Broker | Broker that the client is currently connected to. null is returned if the client is not currently connected to a broker. |
subscriptions
Properties:
Type | Description |
---|---|
Array.<String> | An array containing the topics that the client is currently subscribed to. See Client#subscribe for more information on adding subscriptions. |
Methods
addEventCallback(topic, eventCallback, subscribeToTopicopt)
Adds an event callback to the client for the specified topic. The callback will be invoked when Event messages are received by the client on the specified topic.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
topic |
String | Topic to receive Event messages on. An empty string or null value indicates that the callback should receive messages for all topics (no filtering). |
||
eventCallback |
function | Callback function which should be invoked for a matching message. The first argument passed to the callback function is the Event object. |
||
subscribeToTopic |
Boolean |
<optional> |
true | Whether or not to subscribe for the topic with the broker. |
addRequestCallback(topic, requestCallback, subscribeToTopicopt)
Adds a request callback to the client for the specified topic. The callback will be invoked when Request messages are received by the client on the specified topic. Note that usage of this is quite rare due to the fact that registration of instances with the client occurs automatically when registering a service.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
topic |
String | Topic to receive Request messages on. A empty string or null value indicates that the callback should receive messages for all topics (no filtering). |
||
requestCallback |
function | Callback function which should be invoked for a matching message. The first argument passed to the callback function is the Request object. |
||
subscribeToTopic |
Boolean |
<optional> |
true | Whether or not to subscribe for the topic with the broker. |
addResponseCallback(topic, responseCallback, subscribeToTopicopt)
Adds a response callback to the client for the specified topic. The callback will be invoked when Response messages are received by the client on the specified topic. Note that usage of this is quite rare due to the fact that the use of response callbacks are typically limited to invoking a remote DXL service via the Client#asyncRequest method.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
topic |
String | Topic to receive Response messages on. A empty string or null value indicates that the callback should receive messages for all topics (no filtering). |
||
responseCallback |
function | Callback function which should be invoked for a matching message. The first argument passed to the callback function is the Request object. |
||
subscribeToTopic |
Boolean |
<optional> |
true | Whether or not to subscribe for the topic with the broker. |
asyncRequest(request, responseCallbackopt)
Sends a Request message to a remote DXL service asynchronously. An optional response callback can be specified. This callback will be invoked when the corresponding Response message (or an error) is received by the client.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
request |
Request | The request message to send to a remote DXL service. |
|
responseCallback |
function |
<optional> |
An optional response callback that will be invoked with the result of the request. If an error occurs during the request, the first parameter supplied to the callback contains an Error with failure details. If the response from the DXL fabric to the request includes an ErrorResponse, the first parameter is a RequestError (which contains the error response in its RequestError#dxlErrorResponse property). If the request is successful, the second parameter includes a Response message. |
Throws:
-
If no prior attempt has been made to connect the client via a call to Client#connect.
- Type
- DxlError
connect(callbackopt)
Attempts to connect the client to the DXL fabric. This method returns immediately if a broker has been configured to connect to. The connection is established asynchronously. If provided, the callback function will be invoked the first time a connection has been established to the broker.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
callback |
function |
<optional> |
null | Callback function to invoke when the connection is first established. No arguments are passed to the callback. |
Throws:
-
If no brokers have been specified in the Config passed to the client constructor.
- Type
- DxlError
destroy(callbackopt)
Destroys the client (releases all associated resources).
NOTE: Once the method has been invoked, no other calls should be made to the client.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
callback |
function |
<optional> |
null | An optional callback that will be invoked after client resources have been destroyed. No arguments are passed to the callback. |
disconnect(callbackopt)
Attempts to disconnect the client from the DXL fabric.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
callback |
function |
<optional> |
null | An optional callback that will be invoked when the disconnection attempt is complete. No arguments are passed to the callback. |
registerServiceAsync(serviceRegInfo, callbackopt)
Registers a DXL service with the fabric asynchronously. The specified ServiceRegistrationInfo instance contains information about the service that is to be registered.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
serviceRegInfo |
ServiceRegistrationInfo | A ServiceRegistrationInfo instance containing information about the service that is to be registered. |
||
callback |
function |
<optional> |
null | An optional callback that will be invoked when the registration attempt is complete. If an error occurs during the registration attempt, the first parameter supplied to the callback contains an Error with failure details. |
removeEventCallback(topic, eventCallback)
Removes an event callback from the client for the specified topic. This method must be invoked with the same arguments as when the callback was originally registered via Client#addEventCallback.
Parameters:
Name | Type | Description |
---|---|---|
topic |
String | The topic to remove the callback for. |
eventCallback |
function | The event callback to be removed for the specified topic. |
removeRequestCallback(topic, requestCallback)
Removes a request callback from the client for the specified topic. This method must be invoked with the same arguments as when the callback was originally registered via Client#addRequestCallback.
Parameters:
Name | Type | Description |
---|---|---|
topic |
String | The topic to remove the callback for. |
requestCallback |
function | The request callback to be removed for the specified topic. |
removeResponseCallback(topic, responseCallback)
Removes a response callback from the client for the specified topic. This method must be invoked with the same arguments as when the callback was originally registered via Client#addResponseCallback.
Parameters:
Name | Type | Description |
---|---|---|
topic |
String | The topic to remove the callback for. |
responseCallback |
function | The response callback to be removed for the specified topic. |
sendEvent(event)
Attempts to deliver the specified Event message to the DXL fabric. See Message for more information on message types, how they are delivered to remote clients, etc.
Parameters:
Name | Type | Description |
---|---|---|
event |
Event | The Event to send. |
Throws:
-
If no prior attempt has been made to connect the client via a call to Client#connect.
- Type
- DxlError
sendResponse(response)
Attempts to deliver the specified Response message to the DXL fabric. The fabric will in turn attempt to deliver the response back to the client who sent the corresponding Request.
Parameters:
Name | Type | Description |
---|---|---|
response |
Response | The Response to send. |
Throws:
-
If no prior attempt has been made to connect the client via a call to Client#connect.
- Type
- DxlError
subscribe(topic)
Subscribes to the specified topic on the DXL fabric. This method is typically used in conjunction with the registration of event callbacks via the Client#addEventCallback method.
NOTE: By default when registering an event callback the client will automatically subscribe to the topic. In the example below, the Client#addEventCallback method is invoked with the subscribeToTopic parameter set to false, preventing the automatic subscription.
Parameters:
Name | Type | Description |
---|---|---|
topic |
String | The topic to subscribe to |
Example
client.addEventCallback('/testeventtopic',
function (event) {
console.log('Received event! ' + event.sourceClientId)
}, false)
client.subscribe('/testeventtopic')
unregisterServiceAsync(serviceRegInfo, callbackopt)
Unregisters (removes) a DXL service from the fabric asynchronously. The specified ServiceRegistrationInfo instance contains information about the service that is to be removed.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
serviceRegInfo |
ServiceRegistrationInfo | A ServiceRegistrationInfo instance containing information about the service that is to be unregistered. |
||
callback |
function |
<optional> |
null | An optional callback that will be invoked when the unregistration attempt is complete. If an error occurs during the unregistration attempt, the first parameter supplied to the callback contains an Error with failure details. |
unsubscribe(topic)
Unsubscribes from the specified topic on the DXL fabric. See the Client#subscribe method for more information on subscriptions.
Parameters:
Name | Type | Description |
---|---|---|
topic |
String | The topic to unsubscribe from. |