Lambda API
Lambdas are a special construct in pmesh that enable 1:1 communication between services located on different nodes without the facilitation of the message broker.
The ability to create universally accessible and invocable functions is a powerful feature that can be used to create a wide variety of applications.
One such example is push notifications on a web page. By creating a lambda associated with each browser window, the server can send messages directly to the browser without the need for a message broker.
This reduces the responsibility of the message broker to simply storing 16-byte keys which can be used to directly call the handler without the need for retry logic or durable storage.
Listing local lambdas | /lambda
To list all registered lambdas, simply make a GET request to the /lambda
endpoint.
The result is a map of all registered lambdas, with the key being the lambda id and the value being the lambda object.
Field | Type | Description |
---|---|---|
id | string | The unique identifier of the lambda |
timestamp | string | The time the lambda was created |
code | string | The codec of the lambda |
headers | map[string][]string | The headers of the lambda registration request |
Calling a lambda | /lambda/:id/:method
To invoke a lambda, we simply call the /lambda/:id/:method
endpoint with the payload as the request body.
Response will be the result of the lambda invocation.
Creating a Lambda | /lambda/new/?:id
To create a new lambda, we need a client capable of Websocket communication.
The client should connect to the /lambda/new
endpoint and wait for the open
message, which contains the lambda id.
After the lambda is opened, the client will receive messages in the form of JSON-RPC 1.0 requests, which should be responded accordingly.
The ID parameter in the endpoint is only used if the client is re-opening a lambda. If there is no prior identifier, the client should omit this parameter.
You can see an example of the messages exchanged between the client and the server below.
For a complete implementation of a lambda client using Node.js and the ws
package, see the example below.