json_rpc_2_nats 3.0.4
json_rpc_2_nats: ^3.0.4 copied to clipboard
Utilities to write a client or server using the JSON-RPC 2.0 and Nats
example/main.dart
import 'package:dart_nats/dart_nats.dart' as nats;
import 'package:json_rpc_2/json_rpc_2.dart';
import 'package:stream_channel/stream_channel.dart';
void main() {
var client = nats.Client();
client.connect(Uri.parse('nats://localhost:4222'));
var subscription = client.sub('my.subject');
var server =
Server(subscription.stream.cast<String>() as StreamChannel<String>);
var i = 0;
server.registerMethod('count', () {
// Just return the value to be sent as a response to the client. This can
// be anything JSON-serializable, or a Future that completes to something
// JSON-serializable.
return i++;
});
// Methods can take parameters. They're presented as a `Parameters` object
// which makes it easy to validate that the expected parameters exist.
server.registerMethod('echo', (Parameters params) {
// If the request doesn't have a "message" parameter this will
// automatically send a response notifying the client that the request
// was invalid.
return params['message'].value;
});
// `Parameters` has methods for verifying argument types.
server.registerMethod('subtract', (Parameters params) {
// If "minuend" or "subtrahend" aren't numbers, this will reject the
// request.
return params['minuend'].asNum - params['subtrahend'].asNum;
});
// [Parameters] also supports optional arguments.
server.registerMethod('sort', (Parameters params) {
var list = params['list'].asList;
list.sort();
if (params['descendint'].asBoolOr(false)) {
return list.reversed;
} else {
return list;
}
});
// A method can send an error response by throwing a `RpcException`.
// Any positive number may be used as an application- defined error code.
const dividByZero = 1;
server.registerMethod('divide', (Parameters params) {
var divisor = params['divisor'].asNum;
if (divisor == 0) {
throw RpcException(dividByZero, 'Cannot divide by zero.');
}
return params['dividend'].asNum / divisor;
});
// To give you time to register all your methods, the server won't start
// listening for requests until you call `listen`. Messages are buffered until
// listen is called. The returned Future won't complete until the connection
// is closed.
server.listen();
}