Module websubhub

ballerina/websubhub Ballerina library

0.2.0-beta.2

Overview

This module provides APIs for 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

  1. 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.

  2. 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.
  3. 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.

  4. The publisher notifies the hub of the updates to the topic and the content to deliver is identified.

  5. The hub delivers the identified content to the subscribers of the topic.

Using Hub Client

  • WebSub HubClient can be used to distribute the published content among the subscriber base. The current implementation is based on a ballerina HTTP Client.
Copy
client class HubClient {
    remote function notifyContentDistribution(websubhub:ContentDistributionMessage msg) 
            returns ContentDistributionSuccess | SubscriptionDeletedError | error?
}
  • The following is a sample WebSub HubClient.
Copy
type HubService service object {
    remote function onSubscriptionIntentVerified(websubhub:Subscription msg) {

        // you can pass client config if you want 
        // say maybe retry config
        websub:HubClient hubclient = new(msg);
        check start notifySubscriber(hubclient);
    }

    function notifySubscriber(websubhub:HubClient hubclient) returns error? {
        while (true) {
            // fetch the message from MB
            check hubclient->notifyContentDistribution({
                content: "This is sample content delivery"
            });
        }   
    }
}

Using Publisher Client

  • 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.

Copy
websubhub:PublisherClient publisherClient = new ("http://localhost:9191/websub/hub");

check publisherClient->registerTopic("http://websubpubtopic.com");

var publishResponse = publisherClient->publishUpdate(
            "http://websubpubtopic.com",
            {
                "action": "publish", 
                "mode": "remote-hub"
            });
Returning Errors from Remote Methods
  • Remote functions in websubhub:Service can return error type.
Copy
service /websubhub on new websubhub:Listener(9090) {

    isolated remote function onRegisterTopic(websubhub:TopicRegistration message)
                                returns websubhub:TopicRegistrationSuccess|websubhub:TopicRegistrationError|error {
        boolean validationSuccessfull = check validateRegistration(message);
        if (validationSuccessfull) {
            websubhub:TOPIC_REGISTRATION_SUCCESS;
        } else {
            return websubhub:TOPIC_REGISTRATION_ERROR;
        }
    }

    // implement other remote methods
}

function validateRegistration(websubhub:TopicRegistration message) returns boolean|error {
   // implement validation
}
  • For each remote method error return has a different meaning. Following table depicts the meaning inferred from error returned from all available remote methods.
MethodInterpreted meaning for Error Return
onRegisterTopicTopic registration failure
onDeregisterTopicTopic de-registration failure
onUpdateMessageUpdate message error
onSubscriptionSubscription internal server error
onSubscriptionValidationSubscription validation failure
onSubscriptionIntentVerifiedSubscription intent verification failure
onUnsubscriptionUnsubscription internal server error
onUnsubscriptionValidationUnsubscription validation failure
onUnsubscriptionIntentVerifiedUnsubscription intent verification failure

Import

import ballerina/websubhub;Copy

Metadata

Released date: over 3 years ago

Version: 0.2.0-beta.2


Compatibility

Platform: java11

Ballerina version: slbeta2


Pull count

Total: 32559

Current verison: 0


Weekly downloads


Other versions

See more...