Class: DuplexBrokerClient

DuplexBrokerClient

new DuplexBrokerClient(serializeropt)

Broker client which can publish and subscribe messages in the broker.
Parameters:
Name Type Attributes Default Description
serializer JsonSerializer <optional>
null serializer used to serialize/deserialize messages for/from DuplexBroker. If null then DuplexBrokerClient~BrokerCustomSerializer is used by default. It is the high performance serializer specifically designed just for the interaction with Broker. DuplexBrokerClient~BrokerCustomSerializer is not part of API.
Source:
Example
// Create the duplex output channel.
var anOutputChannel = new WebSocketDuplexOutputChannel("ws://127.0.0.1:8077/MyBroker/", null);

// Create BrokerClient
var aBrokerClient = new DuplexBrokerClient();

// Handler processing notification messages from the broker.
aBrokerClient.onBrokerMessageReceived = onBrokerMessageReceived;

// Attach output channel and be able to send messages and receive responses.
aBrokerClient.attachDuplexOutputChannel(anOutputChannel);

...

// Subscribe to event from the broker.
aBrokerClient.subscribe("MyTimeEvent");

...

// This is how you can unsubscribe.
aBrokerClient.unsubscribe("MyTimeEvent");

// Handler processing notifications from the broker.
function onBrokerMessageReceived(brokerMessageReceivedEventArgs) {

    if (brokerMessageReceivedEventArgs.MessageTypeId == "MyTimeEvent") {
        // Deserialize the event.
        var aValue = JSON.parse(brokerMessageReceivedEventArgs.Message);
        ...
    }
    else if (brokerMessageReceivedEventArgs.MessageTypeId == "SomeOtherEvent") {
    ...
    }
}

...

// You also can send notification events to the broker.
// Broker will forward them to subscribers subscribed for that event.

// Declaring event class.
function MyEvent(value1, value2) {
    this.Value1 = value1;
    this.Value2 = value;
}

// Publishing event via the broker.
var anEvent = new MyEvent(123, 456);
var aSerializedEvent = JSON.stringify(anEvent);
aBrokerClient.sendMessage("MyEvent", aSerializedEvent);

Extends

Methods

_onResponseMessageReceived()

The event is invoked when the response message is received. This event handler method is supposed to be overridden by derived classes.
Inherited From:
Source:

attachDuplexOutputChannel(outputChannel)

Attaches the duplex output channel and opens the connection for sending request messages and receiving response messages.
Please notice when the call returns from this method the connection does not have to be open yet. It can be still in the state opening. Use onConnectionOpened eent to detect when the connetion is open.
Parameters:
Name Type Description
outputChannel WebSocketDuplexOutputChannel
Inherited From:
Source:
Throws:
Throws an error if attaching fails.

detachDuplexOutputChannel()

Detaches the duplex output channel and stops listening to response messages.
Inherited From:
Source:

getAttachedDuplexOutputChannel()

Returns attached duplex output channel.
Inherited From:
Source:

isDuplexOutputChannelAttached()

Returns true if the duplex output channel is attached.
Please notice when the channel is attached it does not have to be connected. E.g. once the output channel was attached and the connection was open and then the connection was broken or closed by input channel.
Inherited From:
Source:

onBrokerMessageReceived(brokerMessageReceivedEventArgs)

The event is invoked when a message is received from the broker.
Parameters:
Name Type Description
brokerMessageReceivedEventArgs BrokerMessageReceivedEventArgs
Source:

onConnectionClosed()

The event which can be subscribed to receive the notification when the connection was closed.
Inherited From:
Source:
Example
// Set your handler to receive close connection notification.
aSender.onConnectionClosed = yourOnConnectionClosed;

onConnectionOpened()

The event which can be subscribed to receive the notification when the connection is open.
Inherited From:
Source:
Example
// Set your handler to receive open connection notification. 
aSender.onConnectionOpened = yourOnConnectionOpened;

sendMessage(eventId, serializedMessage)

Publishes the event via the broker.
It sends the message to the broker. When the broker receives the message it will then notify all subscribers which are subscribed for the specified eventId.
Parameters:
Name Type Description
eventId String identifies the type of the event.
serializedMessage String serialized event data which shall be published via Broker.
Source:

subscribe(eventId)

Subscribes the client for the event or list of events.
Parameters:
Name Type Description
eventId String | Array.<String> identifies event or list of events to be subscribed in the broker.
Source:

unsubscribe(eventId)

Unsubscribes the client from the specified event or list of events.
Parameters:
Name Type Description
eventId string | Array.<string> identifies event or list of events to be unsubscribed from the broker.
Source:

(inner) BrokerCustomSerializer()

Default broker serializer which is highly optimized to serialize/deserialize only BrokerMessage.
Source: