A Quick Guide to EventSource or SEE | Hacker Noon

Pipedream’s EventSource interface is web content’s interface to server-sent events. An EventSource instance offers a persistent connection to an HTTP server, which sends events in text/event-stream format. Data is collected from apps and services such as Github, Twitter, and Google Calendar, and then emitted as individual events via event sources. The API or SSE interfaces can be used to access these events, which trigger associated workflows. You can use a source to trigger workflows or consume its events using PipedREAM’s APIs.

image

Michael Pautov Hacker Noon profile picture

Michael Pautov

Software Engineer

The Eventsource interface is web content’s interface to server-sent events. An Eventsource instance offers a persistent connection to an HTTP server, which sends events in text/event-stream format. Eventsources are mostly used as workflow triggers. You’re adding an event source when you add a new app-based trigger to your workflow.

For two reasons, event sources run as independent resources from workflows:

  • More than one workflow can be triggered by a single event source. You can construct an event source once and use it as the trigger for each process if there is any data source that you intend to use for numerous workflows.
  • You don’t need to perform a process to consume events emitted by event sources in your own application: you can immediately receive the event data via Pipedream’s REST API or a private, real-time SSE stream.

The EventSource() constructor returns a newly-created EventSource, which represents a remote resource.  The basic syntax of  EventSource() is given below.

eventSource = new EventSource(url, configuration);

Example:

var evtSource = new EventSource('sse.php');
var eventList = document.querySelector('ul');
evtSource.onmessage = function (e) {
  var newElement = document.createElement('li');
  newElement.textContent = 'message: ' + e.data;
  eventList.appendChild(newElement);
};

How do event sources work?

Data is collected from apps and services such as Github, Twitter, and Google Calendar, and then emitted as individual events via event sources. The API or SSE interfaces can be used to access these events, which trigger associated workflows.

The event source employs webhooks or another mechanism for real-time data delivery if the service supports them. Google Sheets, for example, offers webhooks, which allow event sources in Google Sheets to send out updates quickly.

Pipedream monitors the API for changes every few minutes if a service doesn’t allow real-time event delivery, and emits events as the API generates them. We poll their API for new records added to a table because Airtable doesn’t support webhooks.

Creating event sources

The Pipedream UI and CLI can both be used to create event sources.

Using the UI to create a source

To create a new event source, go to https://pipedream.com/sources and select the New + button in the top right corner. A list of sources linked to apps (such as Twitter and Github) and generic interfaces will appear (like HTTP).

Select your source, and you’ll be prompted to link any necessary accounts (the Twitter source, for example, requires you to provide Pipedream access to your Twitter account), as well as enter the values for any source-specific configuration options.

You can use a source to trigger Pipedream workflows or consume its events using Pipedream’s APIs once you’ve created it.

Using the CLI to create a source

This will open an interactive menu that will ask you to choose a source. Once you’ve chosen a source, you’ll be prompted to link any required accounts (for example, the Twitter source requires you to provide Pipedream access to your Twitter account) and input the values for any source-specific configuration options.

You can use a source to trigger Pipedream workflows or consume its events using Pipedream’s APIs once you’ve created it.

Taking in events from several sources

The events for a source can be found in the source’s UI, under the Events section.

A Pipedream process can also be started whenever your source produces a new event. This allows you to create workflows for every new tweet, RSS feed item, and more.

Finally, there are a couple of ways to consume events programmatically outside of the Pipedream platform:

  • Using the SSE stream connected to your source in real-time.
  • Using the CLI’s pd events command in real-time.
  • Using the REST API in batches.

Tags

Join Hacker Noon