A repository containing small examples to illustrate the use of Mali for creating gRPC applications.
$ npm install
- Hello World - The stock gRPC Hello World example
- Route Guide - The stock gRPC Route Guide tutorial
- User Service - Sample "User" service
- Secret Service - Process stream of "secret" requests, and consume errors
- Push Server - Push server example
- Errors Example - Example demonstrating sending errors to client
The stock gRPC Hello World example converted to use Mali for server. Both dynamic and static code generated examples are provided. With dynamic we create a server from protocol buffer definition file. With static we create the server from pre-generated Node.js code. The client is unchanged from the gRPC client sample code.
$ # from this directory
$ node ./hello_world_dynamic/greeter_server.js
$ # OR
$ node ./hello_world_static/greeter_server.js
$ # from this directory
$ node ./hello_world_dynamic/greeter_client.js
$ # OR
$ node ./hello_world_static/greeter_client.js
The stock gRPC Route Guide tutorial converted to use Mali for server with some notable changes:
- We use the
route_guide_db.json
as the database source. - All data is asynchronously queried to the file using streaming to demonstrate asynchronous interaction with a "database".
- Similarly Notes are written to
route_guide_db_notes.json
file to simulate asynchronous I/O interactions. This file is truncated at startup to get a clean slate. - Use of logger middleware.
The client is unchanged from the gRPC client sample code.
$ # from within route_guide directory
$ node ./server.js
$ # from this directory
$ node ./route_guide/route_guide_client.js --db_path=./route_guide/route_guide_db.json
A simple "User" service that demonstrates usage of apikey, logger and tojson middleware. As well since currently Protocol Buffers (and therefore gRPC) doesn't support serializing variable JSON data (the user "metadata" field in this case), this example shows how to serialize the property by converting it to bytes. This is not the most efficient solution but the best we can do right now.
$ # from within user_service directory
$ node ./server.js
$ # from this directory
$ npm run test-user
A simple service that asynchronously processes a stream of requests and collects the errors and returns the number of succeeded and the failed details.
$ # from within secret_service directory
$ node ./server.js
$ # from within secret_service directory
$ node ./client.js
gRPC can be used to implement a service that sends updates to all connected clients. This can be achieved using response stream calls. In this contrieved example clients open a stream response connection using syncWidgets
call, which returns all "updated" widgets since some timestamp. Afterwards as widgets are updated the server sends the updates to all the connected clients. The server abstracts and mocks a simple widget store which can be either another service (ie widget service) or a database. Similarly it abstracts and mocks an update notification mechanism that notifies it of updated widgets (for example incoming messages from AMQP or some other messanging system).
$ # from within examples directory
$ node ./push/server.js
$ # from within examples directory
$ node ./push/client.js
$ # from within examples directory
$ node ./push/lotsofclients.js
Demonstrates different ways of comminicating erros to client. Read more.
To run the traditional gRPC implementation:
$ # from within examples directory
$ node ./errors/grpc_server.js
Mali implementation:
$ # from within examples directory
$ node ./errors/mali_server.js
Experiment with different calls to explore.
$ # from within examples directory
$ node ./errors/client.js