-
Notifications
You must be signed in to change notification settings - Fork 9
Description
I'm trying to use electron and node-json-rpc2 as a client and c++ with libjsonrpc (https://github.com/cinemast/libjson-rpc-cpp) as the server. It does not appear that node-json-rpc2 supports notifications per the spec:
4.1 Notification
A Notification is a Request object without an "id" member. A Request object that is a Notification signifies the Client's lack of interest in the corresponding Response object, and as such no Response object needs to be returned to the client. The Server MUST NOT reply to a Notification, including those that are within a batch request.Notifications are not confirmable by definition, since they do not have a Response object to be returned. As such, the Client would not be aware of any errors (like e.g. "Invalid params","Internal error").
Using the server code in step 3 on the libjsonrpc github page and node-json-rpc2 as the client results in an error being returned by libjsonrpc when notifyServer is called. It appears that the node-json-rpc2 client does not provide a means to indicate when a method is a notification and it always sends an id.
Client.prototype.call = function(options, callback, id){
var requestData = {},params=[], method = '', jsonrpc='2.0', id=getRandomId();
if(options){
...
if(options.hasOwnProperty('id')){
id = options.id;
}
}
requestData.id = id;
...
On the server side, it looks like id is always expected and a reply is always returned which should not be the case for notifications.
if(data && data.method && data.params){
if(Object.keys(methods).indexOf(data.method)>-1){
var execMethod = methods[data.method];
reply = execMethod(data.params, data.id);
}
}
res.end(JSON.stringify(reply));
It seems that the code also expects params to always be there although it is optional:
4 Request object
...
params
A Structured value that holds the parameter values to be used during the invocation of the method. This >member MAY be omitted.
...
I've made the following local changes to client.js:
diff --git a/lib/client.js b/lib/client.js
index ebd9cf1..38c9246 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -13,6 +13,7 @@ function Client(options){
this.port = options.port || ((this.protocol==='https') ? 8443 : 8080);
this.agent = (this.protocol==='https')? https : http;
this.method = options.method || "POST";
+ this.notification = options.notification || false;
this.path = options.path || '/';
if(options && options.hasOwnProperty('user') && (options.hasOwnProperty('password') || options.hasOwnProperty('pass'))){
this.authNeeded=true;
@@ -34,11 +35,14 @@ function Client(options){
*
*/
Client.prototype.call = function(options, callback, id){
- var requestData = {},params=[], method = '', jsonrpc='2.0', id=getRandomId();
+ var requestData = {},params=[], method = '', notification=false, jsonrpc='2.0', id=getRandomId();
if(options){
if(options.hasOwnProperty('method')){
method = options.method;
}
+ if(options.hasOwnProperty('notification')){
+ notification = options.notification;
+ }
if(options.hasOwnProperty('params')){
params = options.params;
}
@@ -49,7 +53,9 @@ Client.prototype.call = function(options, callback, id){
id = options.id;
}
}
+ if(!notification){
requestData.id = id;
+ }
requestData.method = method;
requestData.params = params;
requestData.jsonrpc = jsonrpc;