Ballerina Websub module.
This library provides APIs for a WebSub Subscriber Service.
WebSub is a common mechanism for communication between publishers of any kind of Web content and their subscribers, based on HTTP webhooks. Subscription requests are relayed through hubs, which validate and verify the request. Hubs then distribute new and updated content to subscribers when it becomes available. WebSub was previously known as PubSubHubbub.
WebSub Subscriber is an implementation that discovers the hub
and topic URL
of a given resource URL
, subscribes to updates at the hub, and accepts content distribution requests from the hub
.
The subscriber discovers (from the publisher) the topic it needs to subscribe to and the hub(s) that deliver notifications on the updates of the topic.
The subscriber sends a subscription request to one or more discovered hub(s) specifying the discovered topic along
with the other subscription parameters such as:
The hub sends an intent verification request to the specified callback URL. If the response indicates
the verification
(by echoing a challenge specified in the request) by the subscriber, the subscription is added for the topic at the
hub.
The publisher notifies the hub of the updates to the topic and the content to deliver is identified.
The hub delivers the identified content to the subscribers of the topic.
hub
The WebSub Subscriber provides the mechanism to subscribe in a hub
to a given topic URL
.
@websub:SubscriberServiceConfig {
target: ["<HUB_URL>", "<TOPIC_URL>"],
leaseSeconds: 36000
}
service /subscriber on new websub:Listener(9090) {
remote function onSubscriptionValidationDenied(websub:SubscriptionDeniedError msg) returns websub:Acknowledgement? {
// implement subscription validation denied logic here
return websub:ACKNOWLEDGEMENT;
}
remote function onSubscriptionVerification(websub:SubscriptionVerification msg)
returns websub:SubscriptionVerificationSuccess|websub:SubscriptionVerificationError {
// implement subscription intent verification logic here
return websub:SUBSCRIPTION_VERIFICATION_SUCCESS;
}
remote function onUnsubscriptionVerification(websub:UnsubscriptionVerification msg)
returns websub:UnsubscriptionVerificationSuccess|websub:UnsubscriptionVerificationError {
// implement unsubscription intent verification logic here
return websub:UNSUBSCRIPTION_VERIFICATION_SUCCESS;
}
remote function onEventNotification(websub:ContentDistributionMessage event)
returns websub:Acknowledgement|websub:SubscriptionDeletedError? {
// implement on event notification logic here
return websub:ACKNOWLEDGEMENT;
}
}
The WebSub Subscriber also provides the mechanism to discover the hub
and topic URL
resources dynamically via the provided resource URL
and initiates the subscription.
@websub:SubscriberServiceConfig {
target: "RESOURCE_URL",
leaseSeconds: 36000
}
service /subscriber on new websub:Listener(9090) {
remote function onEventNotification(websub:ContentDistributionMessage event)
returns websub:Acknowledgement|websub:SubscriptionDeletedError? {
// implement on event notification logic here
return websub:ACKNOWLEDGEMENT;
}
// other remote methods are optional to be implemented
}
The service path for a WebSub Subscriber is optional. The WebSub Subscriber service has the capability to generate the service path dynamically.
@websub:SubscriberServiceConfig {
target: "RESOURCE_URL",
leaseSeconds: 36000
}
service on new websub:Listener(9090) {
remote function onEventNotification(websub:ContentDistributionMessage event)
returns websub:Acknowledgement|websub:SubscriptionDeletedError? {
// implement on event notification logic here
return websub:ACKNOWLEDGEMENT;
}
// other remote methods are optional to be implemented
}
websub:SubscriberService
locally9090
to the public network via HTTPS
. For information, see the ngrok documentation).
ngrok http -bind-tls=true 9090
Extract the public URL provided by ngrok and provide it as the callback URL for the subscriber service.
@websub:SubscriberServiceConfig {
target: "RESOURCE_URL",
leaseSeconds: 36000,
callback: "<NGROK_PUBLIC_URL>",
appendServicePath: true
}
service on new websub:Listener(9090) {
remote function onEventNotification(websub:ContentDistributionMessage event)
returns websub:Acknowledgement|websub:SubscriptionDeletedError? {
// implement on event notification logic here
return websub:ACKNOWLEDGEMENT;
}
// other remote methods are optional to be implemented
}
hub
@websub:SubscriberServiceConfig {
target: [“https://sample.hub.com“, “https://sample.topic1.com“],
leaseSeconds: 36000,
// By default this is set to false
, hence subscriber on default mode would not initiate unsubscription flow
unsubscribeOnShutdown: true
}
service /subscriber on new websub:Listener(9090, listenerConfigs) {
isolated remote function onEventNotification(websub:ContentDistributionMessage event)
returns websub:Acknowledgement {
// implement logic here
return websub:ACKNOWLEDGEMENT;
}
// other remote methods are optional to be implemented
}
#### Return errors from remote methods
* Remote functions in `websub:SubscriberService` can return `error` type.
```ballerina
@websub:SubscriberServiceConfig {
target: "RESOURCE_URL",
leaseSeconds: 36000,
callback: "<NGROK_PUBLIC_URL>",
appendServicePath: true
}
service on new websub:Listener(9090) {
remote function onEventNotification(websub:ContentDistributionMessage event)
returns websub:Acknowledgement|websub:SubscriptionDeletedError|error? {
boolean isValidRequest = check validateRequest(event);
if isValidRequest {
// implement on event notification logic here
return websub:ACKNOWLEDGEMENT;
}
}
// other remote methods are optional to be implemented
}
function validateRequest(websub:ContentDistributionMessage event) returns boolean|error {
// validation logic
}
error
return has a different meaning. Following table depicts the meaning inferred from error
returned from all available remote methods.Method | Interpreted meaning for Error Return |
---|---|
onSubscriptionValidationDenied | Successfull acknowledgement |
onSubscriptionVerification | Subscription verification failure |
onUnsubscriptionVerification | Unsubscription verification failure |
onEventNotification | Successfull acknowledgement |
Issues and Projects tabs are disabled for this repository as this is part of the Ballerina Standard Library. To report bugs, request new features, start new discussions, view project boards, etc., go to the Ballerina Standard Library parent repository.
This repository only contains the source code for the package.
Download and install Java SE Development Kit (JDK) version 21 (from one of the following locations).
Generate a Github access token with read package permissions, then set the following env
variables:
export packageUser=<Your GitHub Username>
export packagePAT=<GitHub Personal Access Token>
Execute the commands below to build from source.
To build the package:
./gradlew clean build
To build the package without the tests:
./gradlew clean build -x test
To debug the package tests:
./gradlew clean build -Pdebug=<port>
To run a group of tests
./gradlew clean test -Pgroups=<test_group_names>
To debug with Ballerina language:
./gradlew clean build -PbalJavaDebug=<port>
Publish the generated artifacts to the local Ballerina central repository:
./gradlew clean build -PpublishToLocalCentral=true
Publish the generated artifacts to the Ballerina central repository:
./gradlew clean build -PpublishToCentral=true
As an open source project, Ballerina welcomes contributions from the community.
For more information, go to the contribution guidelines.
All contributors are encouraged to read the Ballerina Code of Conduct.
websub
module.