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
@Javascript
Interface, but on macOS it would be within the WebKit messaging protocol, etc.Algorithm
s
be an incoming rawJSON
payloadmsg
be the result of parsings
into key/value pairsmsg.context
exists and is astring
valuemsg.featureName
exists and is astring
valuemsg.method
exists and is astring
valuecontext
,featureName
ormethod
, throw an "invalid format" Exceptionparams
be a reference tomsg.params
or a new, empty key/value structureparams
is not a valid key/value structure, throw an "invalid params" Exceptionmsg.id
field is absent, then:msg
as being of type Messaging.NotificationMessagemsg.id
field is present, then:msg.id
a string value, throw an "invalid params" Exception if it isn'tmsg
as 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
feature
be the result of looking up a feature that matches namemsg.featureName
feature
is not found, throw a "feature not found" Exceptionhandler
be the result of callingfeature.handlerFor(msg.method)
handler
is not found, throw a "handler not found" exceptionhandler
withmsg.params
msg
was marked as a Messaging.NotificationMessage (via step 1), then:{}
msg
was marked as a Messaging.RequestMessage, then:response
be a new instance of Messaging.MessageResponsemsg.context
toresponse.context
msg.featureName
toresponse.featureName
msg.id
toresponse.id
result
be the return value of executinghandler(msg.params)
result
is empty, assignresult
to an empty key/value structureerror
be a new instance of Messaging.MessageErrorerror.message
error
toresponse.error
result
toresponse.result
json
be the string result of convertingresponse
into JSONStep 3) Push-based messaging
event
be a new instance of Messaging.SubscriptionEventevent.context
to the target contextevent.featureName
to the target featureevent.subscriptionName
to the target subscriptionNameparams
be a key/value structureparams
toevent.params
json
be the string result of convertingevent
into JSONNotifications
Messaging.NotificationMessage
Messaging.NotificationMessage with params
Messaging.NotificationMessage with
invalid
paramsRequests
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