Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
HTTP service - HTTP/2 Server push
HTTP/2 server push messages can be sent using the Ballerina http
service. HTTP/2 server push messages allow the server to send resources to the client before the client requests them.
import ballerina/http;
// Create an endpoint with port 9090 to accept HTTP requests.
listener http:Listener http2ServiceEP = new (9090);
service /http2service on http2ServiceEP {
resource function 'default .(http:Caller caller) returns error? {
// Send a push promise.
http:PushPromise promise1 = new (path = "/resource1", method = "GET");
check caller->promise(promise1);
// Send another push promise.
http:PushPromise promise2 = new (path = "/resource2", method = "GET");
check caller->promise(promise2);
// Send one more push promise.
http:PushPromise promise3 = new (path = "/resource3", method = "GET");
check caller->promise(promise3);
// Construct the requested resource.
http:Response res = new;
json msg = {"response": {"name": "main resource"}};
res.setPayload(msg);
// Send the requested resource.
check caller->respond(res);
// Construct promised resource1.
http:Response push1 = new;
msg = {"push": {"name": "resource1"}};
push1.setPayload(msg);
// Push promised `resource1`.
check caller->pushPromisedResponse(promise1, push1);
// Construct promised `resource2`.
http:Response push2 = new;
msg = {"push": {"name": "resource2"}};
push2.setPayload(msg);
// Push promised `resource2`.
check caller->pushPromisedResponse(promise2, push2);
// Construct promised `resource3`.
http:Response push3 = new;
msg = {"push": {"name": "resource3"}};
push3.setPayload(msg);
// Push promised `resource3`.
check caller->pushPromisedResponse(promise3, push3);
}
}
Run the service by executing the following command.
$ bal run http_2_0_server_push.bal
Tip: You can invoke the above service via the Server push client example.
Related links
PreviousHTTP/2 to HTTP/1.1 downgrade
NextServer-sent events