Documentation

Pipet #

JavaScript event streams

POST data to a URL and aggregate it with JavaScript functions

Getting started #

You make an app “Hello world” that lets users greet each other, like ‘Hello’, ‘Hi’, ‘Howdy’, etc. You want to keep track of the unique greetings. You create a stream in your account at https://pipet.io/eric/greetings.

1. POST events from the client #

fetch("https://pipet.io/eric/greetings", {
   body: 'Hello', method: 'POST'
})

2. Get events #

You can GET events or use EventSource to subscribe to new events.

// from browser
fetch("https://pipet.io/eric/greetings[0]") // returns 'Hello'

new EventSource("https://pipet.io/eric/greetings").onmessage = e => {
   console.log(e.id, e.data) // 0, 'Hello'
}

3. Attach a counter reducer #

Reducers run inside Pipet. This counter reducer counts unique greetings.

(state={}, event) => {
   const { data } = event
   state[data] = (state[data] ?? 0) + 1
   // call an API the first time we see 'Hello'
   if (state['Hello'] == 1) request("https://api.slack.com...")
   return state
}

4. Get the reducer state #

Current reducer states can be accessed externally over HTTP.

await (await fetch("https://pipet.io/eric/greetings.counter")).json()
// {"Hello": 1}

What is an event stream? #

○○○○○     a stream is an append-only array of events

○○○○○ ○←  POST to the stream url to append

○○○○○○    attach reducers to the stream to process events
▲ count(state=0, event) { return state + 1 }

○○○○○○    returned value is state for the next iteration
 ▲ state: 1

○○○○○○    until the reducer reaches the last event
     ▲ state: 6

○○○○○○ ○← then, as new events are pushed
     ▲ state: 6

○○○○○○○   the reducer catches up
      ▲ state: 7