SP SPACEPLACE
EXAMPLE #12 PHP Bash

Poll for events since a datetime

The polling pattern for a follower process.

12-events-since.php
#!/usr/bin/env php
<?php

/**
 * Example: Retrieve events since a specific datetime
 *
 * GET /api/events/since?since=<datetime>
 *
 * Returns events ordered oldest-first, ideal for syncing or polling.
 * Optionally filter by source and/or key.
 *
 * Usage:
 *   php 12-events-since.php
 */

$baseUrl       = 'https://www.spaceplace.app';
$dataStreamId  = 'YOUR-DATASTREAM-UUID-HERE';
$tokenValue    = 'YOUR-TOKEN-VALUE-HERE';

// ── Get all events since a specific time ─────────────────────────────────────

$since = '2026-03-30 00:00:00';

$params = http_build_query([
    'since'    => $since,
    'per_page' => 100,
]);

$ch = curl_init("{$baseUrl}/api/events/since?{$params}");
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => [
        'Accept: application/json',
        "X-DataStream-ID: {$dataStreamId}",
        "X-Token: {$tokenValue}",
    ],
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$body = json_decode($response, true);

if ($httpCode === 200) {
    echo "Events since {$since}: {$body['total']} total (page {$body['current_page']}/{$body['last_page']})\n\n";
    foreach ($body['data'] as $event) {
        echo "  [{$event['timestamp']}] {$event['source']}  {$event['key']}={$event['value']}\n";
    }
} else {
    echo "Failed (HTTP {$httpCode}):\n";
    print_r($body);
    exit(1);
}

// ── Polling pattern: track last-seen timestamp ───────────────────────────────

echo "\n--- Polling pattern ---\n";

$lastSeen = '2026-03-31 12:00:00'; // start from a known point

for ($poll = 1; $poll <= 3; $poll++) {
    $params = http_build_query([
        'since'    => $lastSeen,
        'per_page' => 100,
    ]);

    $ch = curl_init("{$baseUrl}/api/events/since?{$params}");
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER     => [
            'Accept: application/json',
            "X-DataStream-ID: {$dataStreamId}",
            "X-Token: {$tokenValue}",
        ],
    ]);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    $body = json_decode($response, true);

    if ($httpCode === 200) {
        $count = count($body['data']);
        echo "Poll #{$poll}: {$count} new event(s) since {$lastSeen}\n";

        // Advance the cursor to the last event's timestamp
        if ($count > 0) {
            $lastEvent = end($body['data']);
            $lastSeen = $lastEvent['timestamp'];
            echo "  Updated cursor to: {$lastSeen}\n";
        }
    } else {
        echo "Poll #{$poll}: failed (HTTP {$httpCode})\n";
    }

    sleep(2); // wait before next poll
}
12-events-since.sh
#!/usr/bin/env bash
#
# Example: Retrieve events since a specific datetime
#
# GET /api/events/since?since=<datetime>
#
# Returns events ordered oldest-first, ideal for syncing or polling.
#
# Usage:
#   bash 12-events-since.sh

BASE_URL="https://www.spaceplace.app"
DATASTREAM_ID="YOUR-DATASTREAM-UUID-HERE"
TOKEN_VALUE="YOUR-TOKEN-VALUE-HERE"

HEADERS=(
  -H "Accept: application/json"
  -H "X-DataStream-ID: ${DATASTREAM_ID}"
  -H "X-Token: ${TOKEN_VALUE}"
)

# ── Get all events since a specific time ──────────────────────────────────────

SINCE="2026-03-30+00:00:00"

echo "--- Events since ${SINCE} ---"

result=$(curl -s "${HEADERS[@]}" "${BASE_URL}/api/events/since?since=${SINCE}&per_page=100")

total=$(echo "$result" | jq '.total')
page=$(echo "$result" | jq '.current_page')
last=$(echo "$result" | jq '.last_page')
echo "${total} total (page ${page}/${last})"
echo "$result" | jq -r '.data[] | "  [\(.timestamp)] \(.source)  \(.key)=\(.value)"'

# ── Polling pattern: track last-seen timestamp ────────────────────────────────

echo ""
echo "--- Polling pattern ---"

LAST_SEEN="2026-03-31+12:00:00"

for poll in 1 2 3; do
  result=$(curl -s "${HEADERS[@]}" \
    "${BASE_URL}/api/events/since?since=${LAST_SEEN}&per_page=100")

  count=$(echo "$result" | jq '.data | length')
  echo "Poll #${poll}: ${count} new event(s) since ${LAST_SEEN}"

  # Advance the cursor to the last event's timestamp
  if [ "$count" -gt 0 ]; then
    LAST_SEEN=$(echo "$result" | jq -r '.data[-1].timestamp')
    echo "  Updated cursor to: ${LAST_SEEN}"
  fi

  sleep 2
done
← All examples