Skip to content

Instantly share code, notes, and snippets.

@forkfork
Last active December 16, 2017 12:19
Show Gist options
  • Save forkfork/3a42f474b0ef0477349d1d522abb9638 to your computer and use it in GitHub Desktop.
Save forkfork/3a42f474b0ef0477349d1d522abb9638 to your computer and use it in GitHub Desktop.
Getting Started - Event Streaming in 10 minutes

Getting Started - Event Streaming in 10 minutes

Wiftycloud instances allow you to manage a stream of events. Events might be customer behaviour such as registration, login, or purchasing.

Concepts

Events are has the follow attributes:

  • An ID, a timestamp with deduplication - for example: 1511695734461-0. This is auto-generated by the server.
  • A set of custom key/value pairs - for example: { action: "login", user: "tim@mit.com" }

Putting these together, an example event may be:

{
  "id": "1511695734461-0",
  "data": {
    "action": "login",
    "user": "tim@mit.com"
  }
}

Events are pushed to a particular stream, which has a maximux number of items. When the maximum number of items (default 100,000) is exceeded, the oldest items on the list will be pushed off to make way for the newer items. All items are however archived and are available in S3.

Authentication tokens must be supplied with all requests. You must supply both the instance, and the authentication token.

Two methods of operation are available: Redis (connecting to your instance on port 6379, or using TLS on port 6380), and via HTTPS. For this guide, we will refer to the HTTPS method.

HTTPS Interface

Writing Events

POST https://myinstance.wiftycloud.com/events - write a message to a stream named 'events'.

To write to a stream, a post body must be sent. Example:

curl -H "auth: aea31232143141aeaea13" -H "Content-Type: application/json" -d '{"action": "login", "user": "tim@mit.com"}' -X POST https://myinstance.wiftycloud.com/events

Readiing Events

GET https://myinstance.wiftycloud.com/events - read messages from the stream named 'events'.

The simplest operation is to read from the start of a particular event stream.

curl -H "auth: aea31232143141aeaea" https://myinstance.wiftycloud.com/events

Response: {
  data: [
    {
      "action": "login",
      "user": "tim@mit.com"
    }
  ],
  "lastId": "1511695734461-0"
}

GET https://myinstance.wiftycloud.com/events?after=15151515151-0 - read messages occurring after the specified ID.

When reading from a stream, the server will return the most recent ID for all events returned. By passing this back to the server, you can request data that occurred after the specified ID. Example:

curl -H "auth: aea31232143141aeaea" https://myinstance.wiftycloud.com/events?after=15151515151-0

Response: {
  data: [],
  "lastId": "1511695734461-0"
}

GET https://myinstance.wiftycloud.com/events?wait=true - read messages, waiting up to 30 seconds for new messages.

If the server has no new data for the client, the server may wait up to 30 seconds for new events to occur. If at the end of 30 seconds no data is available, an empty data response will be returned, along with the lastId if available.

GET https://myinstance.wiftycloud.com/events?count=5 - read messages, returning a maximum of 5 messages.

The default number of messages returned for each call is 10, however any value between 1 and 1000 is allowed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment