Sending request messages to a message path
A client session can send a request message containing typed data to a message path. One or more client sessions can register to handle messages sent to that message path. The handling client session can then send a response message containing typed data. The response message is sent to the requesting client session directly, through the same message path.
- A client session sends a request message to a message path.
- The control client session receives the request message through a request handler.
- The session client session uses sends a response to the request message.
- The client session receives the response.
Both the request message and the response message contain typed values. The messages can contain data of one of the following types: JSON, binary, string, 64-bit integer, or double. The response message is not required to be the same data type as the request it responds to.
Sending to a message path
Required permissions:
permission for the specified message path- The message path to send the request to and receive the response through
- The request message
- The datatype of the request message
- The datatype of the response message
// Example with json topic type. var jsonType = diffusion.datatypes.json(); // Create a JSON object to send as a request. var requestJson = jsonType.from("hello"); // Send the request to a message path "foo". session.messages.sendRequest('foo', requestJson, jsonType).then(function(response) { console.log(response.get()); }, function(error) {});
[session.messaging sendRequest:[PTDiffusionPrimitive requestWithLongLong:42] toPath:message_path int64NumberCompletionHandler:^(NSNumber *response, NSError *error) { if (error) { NSLog(@"Failed to send to %@. Error: %@", message_path, error); } else { NSLog(@"Received response: %@", response); } }];
//Establish client sesssion final Session session = Diffusion.sessions().principal("client").password("password").open("ws://localhost:8080"); //Obtain the Messaging feature final Messaging messaging = session.feature(Messaging.class); //Create a JSON object to send as a request final JSON request = Diffusion.dataTypes().json().fromJsonString("\"hello\""); //Send the request to a message path "foo" and wait for (at most) 5 seconds until the response is received. final JSON response = messaging.sendRequest("foo", request, JSON.class, JSON.class).get(5, TimeUnit.SECONDS);
Responding to request messages sent to a message path
Required permissions:
permission for the specified message path, permission, and permission to register to receive session property values with the request messageDefine a request handler to receive and respond to request messages that have a specific data type.
var jsonType = diffusion.datatypes.json(); var requestJson = jsonType.from({ "foo": "bar"}); var responseJson = jsonType.from({ "ying": "yang"}); // Define a request handler for json topic type var handler = { onRequest: function(request, context, responder) { responder.respond(responseJson, jsonType); }, onError: function() {}, onClose: function() {} };
@interface NumberRequestDelegate : NSObject<PTDiffusionNumberRequestDelegate> @end @implementation NumberRequestDelegate -(void)diffusionTopicTreeRegistration:(PTDiffusionTopicTreeRegistration *)registration didReceiveRequestWithNumber:(nullable NSNumber *)number context:(PTDiffusionRequestContext *)context responder:(PTDiffusionResponder *)responder; { // Do something when a request is received. } - (void)diffusionTopicTreeRegistration:(nonnull PTDiffusionTopicTreeRegistration *)registration didFailWithError:(nonnull NSError *)error { // Do something if the registration fails. } - (void)diffusionTopicTreeRegistrationDidClose:(nonnull PTDiffusionTopicTreeRegistration *)registration { // Do something if the registration closes. }
private final class JSONRequestHandler implements MessagingControl.RequestHandler<JSON, JSON> { @Override public void onClose() { .... } @Override public void onError(ErrorReason errorReason) { .... } @Override public void onRequest(JSON request, RequestContext context, Responder<JSON> responder) { .... responder.respond(response); } }
Register the request handler against a message path. You can only register one request handler against each message path.
var handler = { onRequest: function(request, context, responder) {}, onError: function() {}, onClose: function() {} }; session.messages.addRequestHandler('topic', handler);
// Ensure to maintain a strong reference to your delegate as it // is referenced weakly by the Diffusion client library. NumberRequestDelegate *const delegate = [NumberRequestDelegate new]; PTDiffusionRequestHandler *const handler = [PTDiffusionPrimitive int64RequestHandlerWithDelegate:delegate]; [session.messagingControl addRequestHandler:handler forPath:path completionHandler:^(PTDiffusionTopicTreeRegistration *registration, NSError *error) { // Check error is `nil`, indicating success. }];
messagingControl.addRequestHandler(messagePath, JSON.class, JSON.class, new JSONRequestHandler());
This page last modified: 2020/06/25