Messages will be sent to native platforms in 2 formats - notifications and requests.
Notifications do not require a response, but requests do. The following spec explains the difference and
how to handle each.
The purpose of this library is to enable 3 idiomatic JavaScript methods for communicating with native platforms:
The following describes how you [native engineers] can implement support for this.
Step 1) Receiving a notification or request message:
Each platform will 'receive' messages according to their own best practices, the following spec describes everything after
the message has been delivered from the clientside JavaScript (deliberately avoiding the platform specifics of how messages arrive)
For example, in Android this would be what happens within a @Javascript Interface, but on macOS it would be within
the WebKit messaging protocol, etc.
Algorithm
let s be an incoming raw JSON payload
let msg be the result of parsing s into key/value pairs
2.1 Note: 'parsing' here may not be required if the platform in question receives JSON data directly (ie: JavaScript environments)
if parsing was not successful, throw an "invalid message" Exception
validate that msg.context exists and is a string value
validate that msg.featureName exists and is a string value
validate that msg.method exists and is a string value
6.1 if parsing fails for context, featureName or method, throw an "invalid format" Exception
let params be a reference to msg.params or a new, empty key/value structure
if params is not a valid key/value structure, throw an "invalid params" Exception
Once you've completed Step 1), you'll know whether you are dealing with a notification or a request (something you need
to respond to). At this point you don't know which feature will attempt the message, you just know the format was correct.
Algorithm
let feature be the result of looking up a feature that matches name msg.featureName
if feature is not found, throw a "feature not found" Exception
let handler be the result of calling feature.handlerFor(msg.method)
if handler is not found, throw a "handler not found" exception
Messages will be sent to native platforms in 2 formats - notifications and requests. Notifications do not require a response, but requests do. The following spec explains the difference and how to handle each.
The purpose of this library is to enable 3 idiomatic JavaScript methods for communicating with native platforms:
The following describes how you [native engineers] can implement support for this.
Step 1) Receiving a notification or request message:
Each platform will 'receive' messages according to their own best practices, the following spec describes everything after the message has been delivered from the clientside JavaScript (deliberately avoiding the platform specifics of how messages arrive)
For example, in Android this would be what happens within a
@JavascriptInterface, but on macOS it would be within the WebKit messaging protocol, etc.Algorithm
sbe an incoming rawJSONpayloadmsgbe the result of parsingsinto key/value pairsmsg.contextexists and is astringvaluemsg.featureNameexists and is astringvaluemsg.methodexists and is astringvaluecontext,featureNameormethod, throw an "invalid format" Exceptionparamsbe a reference tomsg.paramsor a new, empty key/value structureparamsis not a valid key/value structure, throw an "invalid params" Exceptionmsg.idfield is absent, then:msgas being of type Messaging.NotificationMessagemsg.idfield is present, then:msg.ida string value, throw an "invalid params" Exception if it isn'tmsgas being of type Messaging.RequestMessageStep 2) Choosing and executing a handler
Once you've completed Step 1), you'll know whether you are dealing with a notification or a request (something you need to respond to). At this point you don't know which feature will attempt the message, you just know the format was correct.
Algorithm
featurebe the result of looking up a feature that matches namemsg.featureNamefeatureis not found, throw a "feature not found" Exceptionhandlerbe the result of callingfeature.handlerFor(msg.method)handleris not found, throw a "handler not found" exceptionhandlerwithmsg.paramsmsgwas marked as a Messaging.NotificationMessage (via step 1), then:{}msgwas marked as a Messaging.RequestMessage, then:responsebe a new instance of Messaging.MessageResponsemsg.contexttoresponse.contextmsg.featureNametoresponse.featureNamemsg.idtoresponse.idresultbe the return value of executinghandler(msg.params)resultis empty, assignresultto an empty key/value structureerrorbe a new instance of Messaging.MessageErrorerror.messageerrortoresponse.errorresulttoresponse.resultjsonbe the string result of convertingresponseinto JSONStep 3) Push-based messaging
eventbe a new instance of Messaging.SubscriptionEventevent.contextto the target contextevent.featureNameto the target featureevent.subscriptionNameto the target subscriptionNameparamsbe a key/value structureparamstoevent.paramsjsonbe the string result of convertingeventinto JSONNotifications
Messaging.NotificationMessage
Messaging.NotificationMessage with params
Messaging.NotificationMessage with
invalidparamsRequests
Messaging.RequestMessage
Messaging.RequestMessage with params
Messaging.RequestMessage with invalid params
Messaging.RequestMessage -> Messaging.MessageResponse with data
Messaging.RequestMessage -> Messaging.MessageResponse with error
Subscriptions
Messaging.SubscriptionEvent without data
Messaging.SubscriptionEvent with data