Skip to content

Interval Polling

wagtail_live.publishers.polling.IntervalPollingPublisher

Interval polling Publisher. Class Based View.

This class handles delivering new updates to the client side (Interval polling technique). It accepts 3 request methods (POST, HEAD and GET) which correspond to the following steps:

  1. The client side initiates (shake) the communication by sending a POST request. The publisher acknowledges by sending relevant data to the client side.

  2. The client side asks if there are any updates by sending a HEAD request. If no updates are available, client side sleeps for the duration of the polling interval and repeats this step.

  3. If new updates are available, client side sends a GET request to get the new updates.

get(self, request, channel_id, *args, **kwargs)

See base class.

Source code in wagtail_live/publishers/polling.py
def get(self, request, channel_id, *args, **kwargs):
    """See base class."""

    live_page = get_object_or_404(self.model, channel_id=channel_id)
    last_update_client = self.get_last_update_client_from_request(request=request)
    tz = timezone.utc if settings.USE_TZ else None
    updated_posts, current_posts = live_page.get_updates_since(
        last_update_ts=datetime.fromtimestamp(last_update_client, tz=tz),
    )

    return JsonResponse(
        {
            "updates": updated_posts,
            "currentPosts": current_posts,
            "lastUpdateTimestamp": live_page.last_update_timestamp,
        }
    )

head(self, request, channel_id, *args, **kwargs)

Sends the timestamp of the last update for the page requested.

Parameters:

Name Type Description Default
request HttpRequest

Client side's request

required
channel_id str

Id of the channel to get last update's timestamp from.

required

Returns:

Type Description
HttpResponse
  • OK: if a page corresponding to the channel_id given exists.

    The timestamp of the last update for the page requested is sent in the response. Client side checks if this value is greater than the last one received. In such case, the client side knows that new updates are available and sends a GET request to get those updates.

  • Http404: else.

Source code in wagtail_live/publishers/polling.py
def head(self, request, channel_id, *args, **kwargs):
    """
    Sends the timestamp of the last update for the page requested.

    Args:
        request (HttpRequest):
            Client side's request
        channel_id (str):
            Id of the channel to get last update's timestamp from.

    Returns:
        HttpResponse:
        - OK: if a page corresponding to the `channel_id` given exists.

            The timestamp of the last update for the page requested is sent in the response.
            Client side checks if this value is greater than the last one received.
            In such case, the client side knows that new updates are available and sends a
            GET request to get those updates.

        - Http404: else.
    """

    live_page = get_object_or_404(self.model, channel_id=channel_id)
    response = JsonResponse(data={}, status=200)
    response["Last-Update-At"] = live_page.last_update_timestamp
    return response

post(self, request, channel_id, *args, **kwargs)

See base class.

Source code in wagtail_live/publishers/polling.py
def post(self, request, channel_id, *args, **kwargs):
    """See base class."""

    live_page = get_object_or_404(self.model, channel_id=channel_id)
    return JsonResponse(
        {
            "livePosts": [live_post.id for live_post in live_page.live_posts],
            "lastUpdateTimestamp": live_page.last_update_timestamp,
            "pollingInterval": get_polling_interval(),
        }
    )