Sending request messages to a session
A client session can send a request message containing typed data directly to a client session. The receiving client session can then send a response message containing typed data. The request and response messages are addressed through the same message path.
- A control client session sends a request message to a client session, specifying the message path to send the message through and the session ID of the client session to send the request message to.
- The client session receives the request message through a request stream.
- The client session uses a responder to send a response to the request message.
- The control 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 a request to a session
Required permissions:
permission for the specified message path and permissionUsually, it is a control client session in your organization's backend that sends messages directly to other sessions.
- The session ID of the client session to send the request to
- The message path to send the request and receive the response through
- The request message
- The datatype of the request message
- The datatype of the response message
control.messages.sendRequest('foo', 'Hello client', session_id, diffusion.datatypes.json(), diffusion.datatypes.json())
[session.messagingControl sendRequest:[PTDiffusionPrimitive requestWithLongLong:42] toSessionId:sessionId path: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 session and control session final Session control = Diffusion.sessions().principal("control").password("password").open("ws://localhost:8080"); final Session client = Diffusion.sessions().principal("client").password("password").open("ws://localhost:8080"); //Obtain the Messaging and MessagingControl features final MessagingControl messagingControl = control.feature("MessagingControl.class"); final Messaging messaging = client.feature(Messaging.class); //Create a JSON object to send as a request final JSON request = Diffusion.dataTypes().json().fromJsonString("\"hello\""); //Create a local request stream for the client to receive direct requests from the control session messaging.setRequestStream("foo", JSON.class, JSON.class, requestStream); //Send the request to a message path "foo" and wait for (at most) 5 seconds until the response is received. final JSON response = messagingControl.sendRequest(client.getSessionId(), "foo", request, JSON.class, JSON.class).get(5, TimeUnit.SECONDS);
Responding to messages sent to a session
Required permissions:
for the specified message pathDefine a request stream to receive and respond to request messages that have a specific data type.
var handler = { onRequest : function(request, context, responder) { .... responder.respond(response); }, onError : function(error) {}, onClose : function() {} }
@interface NumberRequestStreamDelegate : NSObject<PTDiffusionNumberRequestStreamDelegate> @end @implementation NumberRequestStreamDelegate - (void) diffusionStream:(nonnull PTDiffusionStream *)stream didReceiveRequestWithNumber:(nullable NSNumber *)number responder:(nonnull PTDiffusionResponder *)responder { // Do something when a request is received. } - (void)diffusionStream:(nonnull PTDiffusionStream *)stream didFailWithError:(nonnull NSError *)error { // Do something if the stream fails. } - (void)diffusionDidCloseStream:(nonnull PTDiffusionStream *)stream { // Do something if the stream closes. }
private final class JSONRequestStream implements Messaging.RequestStream<JSON, JSON> { @Override public void onClose() { .... } @Override public void onError(ErrorReason errorReason) { .... } @Override public void onRequest(String path, JSON request, Responder<JSON> responder) { .... } }
Add the request stream against a message path. You can only add one request stream for each message path.
control.messages.setRequestStream("foo", diffusion.datatypes.json(), diffusion.datatypes.json(), request_stream);
// Ensure to maintain a strong reference to your request stream as it // is referenced weakly by the Diffusion client library. NumberRequestStreamDelegate *delegate = [NumberRequestStreamDelegate new]; PTDiffusionRequestStream *requestStream = [PTDiffusionPrimitive int64RequestStreamWithDelegate:delegate]; [session.messaging setRequestStream:requestStream forPath:message_path];
messaging.setRequestStream("foo", JSON.class, JSON.class, requestStream);
This page last modified: 2020/06/25