• Stars
    star
    104
  • Rank 330,604 (Top 7 %)
  • Language
    JavaScript
  • Created over 7 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Event Gateway Example

serverless

This example showcases how to develop and deploy Serverless applications using the Event Gateway as the central hub and broker, orchestrating event flows across decoupled services.

  1. General
  2. Step by Step Guide
    1. Getting Started setting up an HTTP endpoint
    2. Subscribing to Custom Events
    3. Error Handling with Event Gateway System Events
    4. Going Multi-Cloud with Google Cloud Functions
  3. Events
  4. Deploy
  5. Resources

General

The Event Gateway allows you to provide HTTP endpoints as well as Pub/Sub functionality into a single event-driven experience. This example uses the serverless run command by the Serverless Framework providing developers with a seamless development experience.

This example contains multiple services. Each of them can be run separately to develop an individual service, but also together at the same time to test the integration between multiple services.

Install

  1. Make sure to have the Serverless Framework installed via npm install -g serverless and you are logged into the Serverless Platform by running serverless login.
  2. Clone down the repository and run npm install in the root directory. This command runs npm install in the frontend app and each backend service.

Step by Step Guide

Getting Started setting up an HTTP endpoint

To get started cd into the users service at services/users and run

serverless run

This will emulate the function register locally. In addition it downloads, runs and configures the Event Gateway to invoke the function every time a request to HTTP endpoint /users is triggered with the method POST. You can test the endpoint by sending an HTTP request to register a user and receive a valid session:

curl -X POST -d '{ "email": "[email protected]" }' --header "Content-Type: application/json" http://localhost:4000/users

In the serverless run terminal session you will see that the user-registered function triggers the user.registered event which we will use in the next step.

Event Gateway  Event 'http' received
Serverless     Function 'users-register' triggered by event 'http'
Event Gateway  Event 'user.registered' received
Serverless     Function 'users-register' finished

Subscribing to Custom Events

Next up open another terminal and cd into the directory of the emails service at services/emails. There also run

serverless run

This will register the function sendWelcomeEmail and subscribe it to the custom event user.registered. Details can be found in your serverless-run process of the users service. By now you have have two services running connected to one Event Gateway.

If you register another user now by running

curl -X POST -d '{ "email": "[email protected]" }' --header "Content-Type: application/json" http://localhost:4000/users

This time the workflow is extended by

Serverless     Function 'emails-sendWelcomeEmail' triggered by event 'user.registered'
Event Gateway  Event 'email.sent' received
Serverless     Function 'emails-sendWelcomeEmail' finished

This event driven architecture allowed us to extend the existing functionality with very little effort.

While this demonstrated how to test the integration between multiple service you can also run and test the emails service alone. For a better experience testing custom events the Serverless Framework supports the emit command. Give it a try by running:

serverless emit -n=user.registered -d='{ "id": 42, "session": "xxxxx", "email": "[email protected]" }'

Error Handling with Event Gateway System Events

The Event Gateway provides the system event gateway.info.functionError which is triggered every time a functions fails.

In order to spin up a function that throws an error you can spin up the crm service at services/crm using:

serverless run

It registers the function addUserToCrm and subscribes it to user.registered. After emitting the event the workflow should include:

Event Gateway  Event 'user.registered' received
Serverless     Function 'crm-addUserToCrm' triggered by event 'user.registered'
Serverless     Function failed due to an error
Event Gateway  Event 'gateway.info.functionError' received

You can subscribe and act on such system events. The errors service at services/errors subscribes to gateway.info.functionError. Once again initialize it with serverless run and emit the user.registered event to see how the alertAdmin is invoked

Event Gateway  Event 'gateway.info.functionError' received
Serverless     Function 'errors-alertAdmin' triggered by event 'gateway.info.functionError'
Serverless     Function 'errors-alertAdmin' finished:

Going Multi-Cloud with Google Cloud Functions

The Event Gateway itself is designed in a way to operate across multiple cloud providers. In our application we leverage this opportunity to deploy two service running Google Cloud Functions using Google's Vision API as well as Google's BigQuery.

Analyzing Images with the Vision API

First you need to get an API and project key from a project that has the Cloud Vision API enabled.

Setup the Vision Service
  1. Go to the Cloud Vision API section of the GCP console. Hit "Enable" to enable Cloud Vision for your project.

  2. Go to the Credentials view in the GCP Console. Hit "Create credentials" and choose "API key". Copy and paste the key into services/vision/config.json.

  3. Retrieve the project id and set PROJECT_ID in the config.json file.

Usage

Next up open a new terminal and go to services/vision/ and run:

serverless run

It registers the function annotateUser and subscribes it to user.registered. After emitting the event this function will fetch the user's Gravatar based on the email address and analyze it with the Vision API.

Once completed the function will emit the event user.annotated. The event can be used by any function subscribing to it from any cloud provider.

Sending Data to BigQuery

Our application emits user.clicked events directly from the front-end. To test this run the front-end, log in and click one of the buttons. Once up and running the analytics service listens to these events and sends them to BigQuery for further analysis.

Setup the Analytics Service

In the next steps you need to retrieve a JSON credentials file with access to BigQuery.

  1. Go to the Google APIs Credentials console. Select your project.

  2. Go to the Google APIs Library page, and click on the BiqQuery API link to make sure the API is enabled for your project.

  3. Click Create Credentials and choose Service account key.

  4. In the Service Account dropdown, choose New service account. Give it a name. In the Role box, go to BigQuery and select BigQuery Admin.

  5. Use a JSON key type and hit 'Create'. It will download a JSON file to your computer. Move it to the directory services/analytics/ as the file name credentials.json.

  6. Update the project name in the config.json file in the services/analytics/ directory with your project name.

Usage

Next up open a new terminal and go to services/analytics/ and run:

node setup.js # setup the BigQuery table
serverless run

Then open another terminal, cd into frontend/ and run:

npm start

This will open your browser and visit http://localhost:3000. Register with an email and click one of the buttons.

In the terminal running the Event Gateway you will recognize a user.clicked event was received. This is the case, because the event was directly emitted from the browser using the Serverless Development Kit (aka FDK).

Using BigQuery

Go to the BigQuery Console. Make sure you're in the right project.

Useful queries:

Show the 1000 most recent events with event name, timestamp, email (if it exists in data), and full data object:

SELECT
  event,
  receivedAt,
  JSON_EXTRACT(data, '$.email') AS email,
  data
FROM
  [serverless-emit:emit_demo.test_events]
ORDER BY receivedAt DESC
LIMIT
  1000

Congratulations! You explored the whole example application.

Please reach out to us in the issues or via email in case you have questions or suggestions for improvement.

List of all Events

A list of all the events used in this application:

  • http
  • user.registered
  • user.clicked
  • user.annotated
  • email.sent
  • gateway.info.functionError

Deploy

Currently work in progress and you can expect this section to be completed soon. Production ready deployment is one of the highest priorities of the Serverless team. Please reach out to us if you are interested to run the Event Gateway on-premise or use a hosted solution.

Resources

More Repositories

1

serverless

⚡ Serverless Framework – Use AWS Lambda and other managed cloud services to build apps that auto-scale, cost nothing when idle, and boast radically low maintenance.
JavaScript
46,101
star
2

examples

Serverless Examples – A collection of boilerplates and examples of serverless architectures built with the Serverless Framework on AWS Lambda, Microsoft Azure, Google Cloud Functions, and more.
JavaScript
11,353
star
3

serverless-graphql

Serverless GraphQL Examples for AWS AppSync and Apollo
JavaScript
2,720
star
4

components

The Serverless Framework's new infrastructure provisioning technology — Build, compose, & deploy serverless apps in seconds...
JavaScript
2,306
star
5

event-gateway

React to any event with serverless functions across clouds
Go
1,647
star
6

serverless-python-requirements

⚡️🐍📦 Serverless plugin to bundle Python packages
JavaScript
1,108
star
7

plugins

Serverless Plugins – Extend the Serverless Framework with these community driven plugins –
JavaScript
970
star
8

aws-ai-stack

AWS AI Stack – A ready-to-use, full-stack boilerplate project for building serverless AI applications on AWS
JavaScript
911
star
9

serverless-graphql-blog

A Serverless Blog leveraging GraphQL to offer a REST API with only 1 endpoint using Serverless v0.5
JavaScript
793
star
10

serverless-plugin-typescript

Serverless plugin for zero-config Typescript support
TypeScript
776
star
11

github-action

⚡:octocat: A Github Action for deploying with the Serverless Framework
Dockerfile
642
star
12

scope

🔭 Scope - Create a birdeye's view of your Github project and embed on your site
JavaScript
456
star
13

guide

Serverless Guide - An open-source definitive guide to serverless architectures.
Shell
439
star
14

serverless-kubeless

This plugin enables support for Kubeless within the Serverless Framework.
JavaScript
302
star
15

serverless-google-cloudfunctions

Serverless Google Cloud Functions Plugin – Adds Google Cloud Functions support to the Serverless Framework
JavaScript
272
star
16

serverless-azure-functions

Serverless Azure Functions Plugin – Add Azure Functions support to the Serverless Framework
TypeScript
266
star
17

post-scheduler

Schedule posts & content updates for static websites (Jekyll, Hugo, Gatsby, Phenomic etc)
JavaScript
191
star
18

serverless-starter

A boilerplate for new Serverless Projects
JavaScript
190
star
19

blog

serverless.com/blog – Posts from the Serverless community & core team. Contributions welcome!
JavaScript
183
star
20

serverless-client-s3

A plugin to deploy front-end assets to S3 via the Serverless Framework
JavaScript
171
star
21

serverless-websockets-plugin

Websocket support for Serverless Framework on AWS
JavaScript
149
star
22

serverless-openwhisk

Adds Apache OpenWhisk support to the Serverless Framework!
JavaScript
143
star
23

typescript

TypeScript definitions for Serverless Framework service configuration
TypeScript
140
star
24

serverless-slackbot

[Deprecated] A Serverless Module to create your own SlackBot without servers/websockets via Slash Commands
JavaScript
135
star
25

cloud

Serverless Cloud makes building, deploying, and managing serverless applications easier and more accessible to everyone.
HTML
112
star
26

compose

Orchestrate Serverless Framework in monorepos
JavaScript
109
star
27

serverless-optimizer-plugin

Serverless Optimizer Plugin: Optimizers for reducing Lambda file sizes and improving their performance -
JavaScript
102
star
28

enterprise

Documentation for the Serverless Framework Enterprise Edition –
95
star
29

multicloud

The serverless @multicloud library provides an easy way to write your serverless handlers 1 time and deploy them to multiple cloud providers include Azure & AWS.
TypeScript
90
star
30

aws-event-mocks

JavaScript
83
star
31

emulator

Serverless Emulator which lets you run serverless functions locally
JavaScript
78
star
32

serverless-local-schedule

⚡️🗺️⏰ Schedule AWS CloudWatch Event based invocations in local time(with DST support!)
JavaScript
72
star
33

event-mocks

TypeScript
72
star
34

fdk

This library is deprecated. Please use https://github.com/serverless/event-gateway-sdk instead.
JavaScript
69
star
35

serverless-open-runtime

WIP - An extensible, community-driven, open-runtime for serverless compute
Go
68
star
36

serverless-runtime-babel

Babel runtime for v.0.5 of the Serverless Framework
JavaScript
68
star
37

forms-service

Serverless Forms Service to collect form data with Admin UI
JavaScript
66
star
38

serverless-secrets-plugin

JavaScript
66
star
39

serverless-golang

Serverless Template for Golang
Go
64
star
40

serverless-tencent

⚡️ 🐧 Serverless Tencent CLI 及中文讨论社区
JavaScript
63
star
41

serverless-slack

A Serverless Module featuring pre-written Slack functions from authorization to slash commands!
JavaScript
49
star
42

dashboard-plugin

The Serverless Framework Dashboard plugin
JavaScript
49
star
43

event-gateway-getting-started

Walkthrough application for using the Event Gateway. -- https://www.serverless.com
JavaScript
48
star
44

workshop

A complete full-stack application and walkthrough for learning the features of the Serverless Framework.
CSS
45
star
45

desktop

A native GUI application that makes it easy to explore and test Serverless Framework applications built on AWS Lambda.
45
star
46

serverless-swagger-plugin

JavaScript
44
star
47

serverless-meta-sync

Secure syncing of serverless project meta data across teams
JavaScript
44
star
48

event-gateway-sdk

Event Gateway JavaScript SDK
JavaScript
43
star
49

safeguards-plugin

Serverless Framework Plugin to enforce safeguard policies
JavaScript
33
star
50

serverless-graphql-relay

Serverless GraphQL Boilerplate using Relay – Ready to be deployed to production within minutes …
JavaScript
32
star
51

utils

General serverless utilities
JavaScript
29
star
52

serverless-event-gateway-plugin

Event Gateway plugin for Serverless Framework
JavaScript
28
star
53

eslint-config

Common ESLint & Prettier config for Serverless projects
JavaScript
27
star
54

serverless-knative

Serverless Knative Provider Plugin – Adds Knative to the Serverless Framework
JavaScript
25
star
55

serverless-plugin-boilerplate

A Starter Project to help you build Plugins for the Serverless Framework -
JavaScript
24
star
56

dashboard

JavaScript
24
star
57

fullstack-course

The entire fullstack application course as seen on serverless.com/learn
JavaScript
24
star
58

serverless-babel-plugin

A Serverless plugin to compile your JavaScript code with Babel before deployment
JavaScript
23
star
59

serverless-plugin-log-retention

Control the retention of your serverless function's cloudwatch logs.
JavaScript
20
star
60

cloud-computing-conferences

A curated list of all the best cloud computing conferences happening in 2019! Check here to find a serverless or cloud event happening near you.
18
star
61

event-gateway-workshop

Learn what the Event Gateway is, how it works and build your first event-driven multi-cloud application!
JavaScript
18
star
62

fdk-tmp

The Serverless Function Development Kit (FDK)
JavaScript
18
star
63

aws-sdk-extra

The AWS SDK + a handful of extra convenience methods.
JavaScript
18
star
64

platform-sdk

Serverless Platform SDK
JavaScript
17
star
65

meetups

Want to start a Serverless usergroup? We can help you get started and be an official Serverless Usergroup. Join us to be part of the family of many Serverless Usergroups across the world.
17
star
66

boilerplate-googlecloudfunctions-nodejs

A Serverless Framework Boilerplate for Google Cloud Functions support in Node.js
JavaScript
15
star
67

cli

Serverless Components CLI
JavaScript
14
star
68

serverless-helpers-py

Python
12
star
69

tutorial

A tutorial repo with multiple projects designed to help teach users
JavaScript
11
star
70

serverless-helpers-js

Serverless Helpers Node.Js: An NPM module that provides helper functions for Serverless Modules written in Node.js -
JavaScript
11
star
71

template

Compose & provision a collection of Serverless Components
JavaScript
10
star
72

template-package

Repository template for serverless packages
JavaScript
9
star
73

serverlessconf-workshop

JavaScript
9
star
74

serverless-search-engine

Part of the Serverless Live series where we build a very simplistic search engine and spider
JavaScript
9
star
75

artwork

Official design elements of Serverless Inc. and its projects
8
star
76

extensions

Use / make / monetize developer experiences.
JavaScript
8
star
77

event-gateway-connector

Serverless Event Gateway Connector Framework
Go
7
star
78

raffleapp

A serverless raffle app
JavaScript
7
star
79

netlify-landing-page

Components Landing page demo
CSS
7
star
80

compose-example

JavaScript
6
star
81

demo-todo

An example serverless-todo app
JavaScript
5
star
82

serverless-raffle

JavaScript
5
star
83

enterprise-template

JavaScript
5
star
84

workshops

Companion repo for workshops with instructions and code examples
JavaScript
5
star
85

sls-action

GitHub action for Serverless CLI
TypeScript
5
star
86

test

Test setup and utilities for serverless projects
JavaScript
4
star
87

compose-website-example

JavaScript
4
star
88

console

4
star
89

cli-design

Repository to discuss Serverless Framework v3 redesign
HTML
4
star
90

event-gateway-tracer

JavaScript
3
star
91

core

Serverless Components Core
JavaScript
3
star
92

status

The Serverless Status Page
JavaScript
3
star
93

partner-examples

JavaScript
2
star
94

components-core

WIP
TypeScript
2
star
95

azure-build-task

A Serverless build task for Azure Dev Ops
TypeScript
1
star
96

sfe-nodejs-service

JavaScript
1
star
97

inquirer

inquirer with enforced serverless theme
JavaScript
1
star
98

template-getting-started-node-js

Application templates used by the Serverless Platform
1
star
99

serverless-cloud-gitpod

Starter repository for running Serverless Cloud on GitPod
1
star
100

user-testing

Tests of product concepts
JavaScript
1
star