websubhub
Module websubhub

ballerina/websubhub
Overview
This module provides APIs for a WebSub Hub service and WebSub Publisher client.
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 requests. Hubs then distribute new and updated content to subscribers when it becomes available. WebSub was previously known as PubSubHubbub.
WebSub Hub is an implementation that handles subscription requests and distributes the content to subscribers when the corresponding topic URL has been updated.
WebSub Publisher is an implementation that advertises a topic and hub URL on one or more resource URLs.
Basic flow with WebSub
-
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 other subscription parameters such as:
- The callback URL to which the content is expected to be delivered.
- (Optional) The lease period (in seconds) the subscriber wants the subscription to stay active.
- (Optional) A secret to use for authenticated content distribution.
-
The hub sends an intent verification request to the specified callback URL. If the response indicates 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.
Use websubhub:HubClient
- WebSub HubClient can be used to distribute the published content to subscribers. The current implementation is based on
Ballerina HTTP Client
.
- The following is a sample usage for WebSub HubClient.
Use websubhub:PublisherClient
-
The
PublisherClient
APIs are defined by Ballerina and it has no connection with the WebSub specification. As mentioned earlier, even though the WebSub specification extensively discusses the relationship between the subscriber and hub, it does not discuss much the relationship between the publisher and hub. -
The following is a sample WebSub Publisher Client.
Return errors from remote methods
- Remote functions in
websubhub:Service
can returnerror
type.
- For each remote method
error
return has a different meaning. Following table depicts the meaning inferred fromerror
returned from all available remote methods.
Method | Interpreted meaning for Error Return |
---|---|
onRegisterTopic | Topic registration failure |
onDeregisterTopic | Topic de-registration failure |
onUpdateMessage | Update message error |
onSubscription | Subscription internal server error |
onSubscriptionValidation | Subscription validation failure |
onSubscriptionIntentVerified | Subscription intent verification failure |
onUnsubscription | Unsubscription internal server error |
onUnsubscriptionValidation | Unsubscription validation failure |
onUnsubscriptionIntentVerified | Unsubscription intent verification failure |
Listeners
websubhub: Listener
Represents a Service listener endpoint.
Constructor
Initiliazes the websubhub:Listener
instance.
listener websubhub:Listener hubListenerEp = check new (9090);
init (int|Listener listenTo, *ListenerConfiguration config)
- config *ListenerConfiguration - Custom
websubhub:ListenerConfiguration
to be provided to the underlying HTTP listener
attach
Attaches the provided websubhub:Service
to the websubhub:Listener
.
Parameters
- 'service Service - The
websubhub:Service
object to attach
Return Type
- Error? - An
websubhub:Error
if an error occurred during the service attaching process or else()
detach
Detaches the provided websubhub:Service
from the websubhub:Listener
.
Parameters
- s Service - The
websubhub:Service
object to be detached
Return Type
- Error? - An
websubhub:Error
if an error occurred during the service detaching process or else()
'start
function 'start() returns Error?
Starts the registered service programmatically.
Return Type
- Error? - An
websubhub:Error
if an error occurred during the listener-starting process or else()
gracefulStop
function gracefulStop() returns Error?
Gracefully stops the hub listener. Already-accepted requests will be served before the connection closure.
Return Type
- Error? - An
websubhub:Error
if an error occurred during the listener-stopping process
immediateStop
function immediateStop() returns Error?
Stops the service listener immediately.
Return Type
- Error? - An
websubhub:Error
if an error occurred during the listener-stopping process or else()
Clients
websubhub: HubClient
HTTP Based client for WebSub content publishing to subscribers
Constructor
Initializes the websubhub:HubClient
.
websubhub:HubClient hubClientEP = check new({
hub: "https://hub.com",
hubMode: "subscribe",
hubCallback: "http://subscriber.com/callback",
hubTopic: "https://topic.com",
hubSecret: "key"
});
init (Subscription subscription, *ClientConfiguration config)
- subscription Subscription - Original
websubhub:Subscription
record, which contains the details of thesubscriber
- config *ClientConfiguration - The
websubhub:ClientConfiguration
for the underlying client
notifyContentDistribution
function notifyContentDistribution(ContentDistributionMessage message) returns ContentDistributionSuccess|SubscriptionDeletedError|Error
Distributes the published content to the subscribers.
Parameters
- message ContentDistributionMessage - Content to be distributed to the topic subscriber
Return Type
- ContentDistributionSuccess|SubscriptionDeletedError|Error - An
websubhub:Error
if an exception occurred, awebsubhub:SubscriptionDeletedError
if the subscriber responded withHTTP 410
, or else awebsubhub:ContentDistributionSuccess
for successful content delivery
websubhub: PublisherClient
The HTTP based client for WebSub topic registration and deregistration, and notifying the hub of new updates.
Constructor
Initializes the websub:PublisherClient
.
websub:PublisherClient publisherClient = check new("https://sample.hub.com");
init (string url, *ClientConfiguration config)
- url string - The URL to publish/notify updates
- config *ClientConfiguration - The
websubhub:ClientConfiguration
for the underlying client or else()
registerTopic
function registerTopic(string topic) returns TopicRegistrationSuccess|TopicRegistrationError
Registers a topic in a Ballerina WebSub Hub to which the subscribers can subscribe and the publisher will publish updates.
Parameters
- topic string - The topic to register
Return Type
- TopicRegistrationSuccess|TopicRegistrationError - A
websubhub:TopicRegistrationError
if an error occurred registering the topic or elsewebsubhub:TopicRegistrationSuccess
deregisterTopic
function deregisterTopic(string topic) returns TopicDeregistrationSuccess|TopicDeregistrationError
Deregisters a topic in a Ballerina WebSub Hub.
Parameters
- topic string - The topic to deregister
Return Type
- TopicDeregistrationSuccess|TopicDeregistrationError - A
websubhub:TopicDeregistrationError
if an error occurred un registering the topic or elsewebsubhub:TopicDeregistrationSuccess
publishUpdate
function publishUpdate(string topic, map<string>|string|xml|json|byte[] payload, string? contentType) returns Acknowledgement|UpdateMessageError
Publishes an update to a remote Ballerina WebSub Hub.
Parameters
- topic string - The topic for which the update occurred
- contentType string? (default ()) - The type of the update content to set as the
ContentType
header
Return Type
- Acknowledgement|UpdateMessageError - A
websubhub:UpdateMessageError
if an error occurred with the update or elsewebsubhub:Acknowledgement
notifyUpdate
function notifyUpdate(string topic) returns Acknowledgement|UpdateMessageError
Notifies a remote WebSubHub from which an update is available to fetch for hubs that require publishing.
Parameters
- topic string - The topic for which the update occurred
Return Type
- Acknowledgement|UpdateMessageError - A
websubhub:UpdateMessageError
if an error occurred with the notification or elsewebsubhub:Acknowledgement
Classes
websubhub: StatusOK
Response status OK.
Fields
- code int - Status code value
- code STATUS_OK(default http:STATUS_OK) - Status code for action
websubhub: StatusPermanentRedirect
Response status Permanent Redirect.
Fields
- code int - Status code value
- code STATUS_PERMANENT_REDIRECT(default http:STATUS_PERMANENT_REDIRECT) - Status code for action
websubhub: StatusTemporaryRedirect
Response status Temporary Redirect.
Fields
- code int - Status code value
- code STATUS_TEMPORARY_REDIRECT(default http:STATUS_TEMPORARY_REDIRECT) - Status code for action
Object types
websubhub: Service
The WebSubHub service type.
Records
websubhub: Acknowledgement
Record to represent the acknowledgement of content updated by the publisher.
Fields
- Fields Included from * CommonResponse
- statusCode int(default http:STATUS_OK) - HTTP status code for the response
websubhub: ClientConfiguration
Record to represent the client configuration for the HubClient/PublisherClient.
Fields
- httpVersion HttpVersion(default HTTP_1_1) - The HTTP version understood by the client
- http1Settings ClientHttp1Settings(default {}) - Configurations related to HTTP/1.x protocol
- http2Settings ClientHttp2Settings(default {}) - Configurations related to HTTP/2 protocol
- timeout decimal(default 60) - The maximum time to wait (in seconds) for a response before closing the connection
- poolConfig PoolConfiguration? - Configurations associated with request pooling
- auth ClientAuthConfig? - Configurations related to client authentication
- retryConfig RetryConfig? - Configurations associated with retrying
- responseLimits ResponseLimitConfigs(default {}) - Configurations associated with inbound response size limits
- secureSocket ClientSecureSocket? - SSL/TLS related options
- circuitBreaker CircuitBreakerConfig? - Configurations associated with the behaviour of the Circuit Breaker
websubhub: CommonResponse
Record to represent the parent type for all the response records.
Fields
- statusCode int - HTTP status code for the response
- mediaType string?(default ()) - Content-Type of the request received
websubhub: ContentDistributionMessage
Record to represent a WebSub content delivery.
Fields
- contentType string?(default ()) - The content-type of the payload
websubhub: ContentDistributionSuccess
Record to represent the successful WebSub content delivery.
Fields
- Fields Included from * CommonResponse
websubhub: ListenerConfiguration
Provides a set of configurations for configure the underlying HTTP listener of the WebSubHub listener.
Fields
- Fields Included from * ListenerConfiguration
- host string
- http1Settings ListenerHttp1Settings
- secureSocket ListenerSecureSocket|()
- httpVersion HttpVersion
- timeout decimal
- server string|()
- requestLimits RequestLimitConfigs
- interceptors Interceptor[]
- gracefulStopTimeout decimal
- socketConfig ServerSocketConfig
websubhub: ServiceConfiguration
Configuration for a WebSub Hub service.
Fields
- leaseSeconds int? - The period for which the subscription is expected to be active in the
hub
- webHookConfig ClientConfiguration? - HTTP client configurations for subscription/unsubscription intent verification
websubhub: Subscription
Record to represent the subscription request body.
Fields
- hub string - URL of the
hub
to which the subscriber has subscribed
- hubMode string - Current
hub
action
- hubCallback string - Callback URL for subscriber to receive distributed content
- hubTopic string - Topic to which subscriber has subscribed
- hubLeaseSeconds string?(default ()) - Amount of time in seconds during when the subscription is valid
- hubSecret string?(default ()) - Secret Key to sign the distributed content
websubhub: SubscriptionAccepted
Record to represent the subscription accepted by the hub
.
Fields
- Fields Included from * CommonResponse
- statusCode int(default http:STATUS_ACCEPTED) - HTTP status code for the response
websubhub: SubscriptionPermanentRedirect
Record to represent the permanent subscription redirects.
Fields
- redirectUrls string[] - URLs to which the subscription has been redirected
- statusCode int -
- mediaType string|() -
- anydata... -
- codereadonly STATUS_PERMANENT_REDIRECT(default http:STATUS_PERMANENT_REDIRECT) - Status code for the action
websubhub: SubscriptionTemporaryRedirect
Record to represent temporary subscription redirects.
Fields
- redirectUrls string[] - URLs to which the subscription has been redirected
- statusCode int -
- mediaType string|() -
- anydata... -
- codereadonly STATUS_TEMPORARY_REDIRECT(default http:STATUS_TEMPORARY_REDIRECT) - Status code of the action
websubhub: TopicDeregistration
Record to represent the topic-deregistration request body.
Fields
- topic string -
Topic
, which should be unregistered from thehub
- hubMode string(default MODE_DEREGISTER) - Current
hub
action
websubhub: TopicDeregistrationSuccess
Record to represent the successful topic deregistration.
Fields
- Fields Included from * CommonResponse
- statusCode int(default http:STATUS_OK) - HTTP status code for the response
websubhub: TopicRegistration
Record to represent the topic-registration request body.
Fields
- topic string -
Topic
, which should be registered in thehub
- hubMode string(default MODE_REGISTER) - Current
hub
action
websubhub: TopicRegistrationSuccess
Record to represent the successful topic registration.
Fields
- Fields Included from * CommonResponse
- statusCode int(default http:STATUS_OK) - HTTP status code for the response
websubhub: Unsubscription
Record to represent the unsubscription request body.
Fields
- hubMode string - Current
hub
action
- hubCallback string - Callback URL for subscriber to received distributed content
- hubTopic string - Topic from which subscriber wants to unsubscribe
- hubSecret string?(default ()) - Secret Key to sign the distributed content
websubhub: UnsubscriptionAccepted
Record to represent the unsubscription acceptance.
Fields
- Fields Included from * CommonResponse
- statusCode int(default http:STATUS_ACCEPTED) - HTTP status code for the response
websubhub: UpdateMessage
Record to represent the content update message.
Fields
- msgType MessageType - Type of the content update message
- hubTopic string - Topic of which the content should be updated
- contentType string - Content-Type of the update-message
websubhub: VerifiedSubscription
Record to represent the completed subscription.
Fields
- Fields Included from * Subscription
- verificationSuccess boolean(default true) - Flag to notify whether a subscription verification is successfull
websubhub: VerifiedUnsubscription
Record to represent a completed unsubscription.
Fields
- Fields Included from * Unsubscription
- verificationSuccess boolean(default true) - Flag to notify whether a unsubscription verification is successfull
Enums
websubhub: MessageType
Enum to differentiate the type of the content-update message.
Members
topic
topic
Constants
websubhub: COMPRESSION_ALWAYS
Always set accept-encoding/content-encoding in outbound request/response.
websubhub: COMPRESSION_AUTO
When service behaves as a HTTP gateway inbound request/response accept-encoding option is set as the outbound request/response accept-encoding/content-encoding option.
websubhub: COMPRESSION_NEVER
Never set accept-encoding/content-encoding header in outbound request/response.
Annotations
websubhub: ServiceConfig
WebSub Hub Configuration for the service.
Types
websubhub: Compression
Options to compress using Gzip or deflate.
AUTO
: When service behaves as a HTTP gateway inbound request/response accept-encoding option is set as the
outbound request/response accept-encoding/content-encoding option
ALWAYS
: Always set accept-encoding/content-encoding in outbound request/response
NEVER
: Never set accept-encoding/content-encoding header in outbound request/response
Variables
websubhub: TOPIC_REGISTRATION_SUCCESS
Common response, which could be used for websubhub:TopicRegistrationSuccess
.
websubhub: TOPIC_REGISTRATION_ERROR
Common response, which could be used for websubhub:TopicRegistrationError
.
websubhub: TOPIC_DEREGISTRATION_SUCCESS
Common response, which could be used for websubhub:TopicDeregistrationSuccess
.
websubhub: TOPIC_DEREGISTRATION_ERROR
Common response, which could be used for websubhub:TopicDeregistrationError
.
websubhub: ACKNOWLEDGEMENT
Common response, which could be used for websubhub:Acknowledgement
.
websubhub: UPDATE_MESSAGE_ERROR
Common response, which could be used for websubhub:UpdateMessageError
.
websubhub: SUBSCRIPTION_ACCEPTED
Common response, which could be used for websubhub:SubscriptionAccepted
.
websubhub: BAD_SUBSCRIPTION_ERROR
Common response, which could be used for websubhub:BadSubscriptionError
.
websubhub: INTERNAL_SUBSCRIPTION_ERROR
Common response, which could be used for websubhub:InternalSubscriptionError
.
websubhub: SUBSCRIPTION_DENIED_ERROR
Common response, which could be used for websubhub:SubscriptionDeniedError
.
websubhub: UNSUBSCRIPTION_ACCEPTED
Common response, which could be used for websubhub:UnsubscriptionAccepted
.
websubhub: BAD_UNSUBSCRIPTION_ERROR
Common response, which could be used for websubhub:BadUnsubscriptionError
.
websubhub: INTERNAL_UNSUBSCRIPTION_ERROR
Common response, which could be used for websubhub:InternalUnsubscriptionError
.
websubhub: UNSUBSCRIPTION_DENIED_ERROR
Common response, which could be used for websubhub:UnsubscriptionDeniedError
.
Errors
websubhub: BadSubscriptionError
Error type representing the errors in the subscription request.
websubhub: BadUnsubscriptionError
Error type representing the errors in the unsubscription request.
websubhub: ContentDeliveryError
Error type representing the internal errors in the content distribution.
websubhub: Error
Represents a websubhub distinct error.
websubhub: InternalSubscriptionError
Error type representing the internal errors in the subscription action.
websubhub: InternalUnsubscriptionError
Error type representing the internal errors in the unsubscription action.
websubhub: ServiceExecutionError
Represents a websubhub service execution error.
websubhub: SubscriptionDeletedError
Error type representing the subscriber ending the subscription
by sending HTTP 410
for the content delivery response.
websubhub: SubscriptionDeniedError
Error type representing the validation errors in the subscription request body.
websubhub: TopicDeregistrationError
Error type representing the errors in the topic unregistration action.
websubhub: TopicRegistrationError
Error type representing the errors in the topic registration action.
websubhub: UnsubscriptionDeniedError
Error type representing the validation errors in the unsubscription request body.
websubhub: UpdateMessageError
Error type representing the errors in the content update request.