GithubHelp home page GithubHelp logo

fastly / compute-starter-kit-javascript-queue Goto Github PK

View Code? Open in Web Editor NEW
5.0 14.0 1.0 6.03 MB

Queuing package template for JavaScript based Fastly Compute projects. Park your users in a virtual queue to reduce the demand on your origins during peak times.

Home Page: https://fastly.dev/solutions/demos/compute-stateful-queue/

License: MIT License

JavaScript 75.78% CSS 13.30% HTML 10.92%
compute-starter fastly-oss-tier1

compute-starter-kit-javascript-queue's Introduction

Queuing Starter Kit for JavaScript Deploy to Fastly

Park website visitors in a virtual queue to reduce the demand on your origins during peak times.

For more details about other starter kits for Fastly Compute, see the Fastly Documentation Hub

A screenshot of the queue page for a ticketing website. It reads: Sometimes we have to restrict the number of people who can access our website at the same time, so that everything works properly. There are 46 people ahead of you in the queue.

Features

  • Park visitors in a queue to limit the amount of active users on the website โณ
  • Ship queue analytics to log endpoints ๐Ÿ”Ž
  • Allow certain requests such as robots.txt, favicon to bypass the queue ๐Ÿค–
  • No client-side scripting required โšก๏ธ

Getting started

  1. If you haven't already, sign up for Upstash and create a Redis service.
  2. Initialize a Compute project using this starter kit.
    fastly compute init --from=https://github.com/fastly/compute-starter-kit-javascript-queue
  3. Create the upstash backend, changing the default hostname to the one provided in the Upstash console.
  4. Create the protected_content backend by accepting the default example host or setting your own.
  5. Populate the config config store by following the prompts to configure Upstash and set a secret for signing cookies.
  6. Run fastly compute publish to deploy your queue.

Understanding the code

This starter is fully-featured, and requires some dependencies on top of the @fastly/js-compute npm package. The following is a list of the dependencies used:

  • @upstash/redis - a REST-based Redis client for storing queue state. You could easily swap this for your own storage backend.
  • jws - a library for generating and validating JSON Web Tokens.

The starter will require a backend to be configured to send requests to once visitors have made it through the queue. For demonstration, the default is a public S3 bucket with some assets and an index page.

The template uses webpack to bundle index.js and its imports into a single JS file, bin/index.js, which is then wrapped into a .wasm file, bin/index.wasm using the js-compute-runtime CLI tool bundled with the @fastly/js-compute npm package, and bundled into a .tar.gz file ready for deployment to Compute.

Design

When a visitor makes a request for the first time, we generate a signed JWT containing their position in the queue, which is determined by fetching the current queue length from Redis (INCR queue:length). This signed JWT is sent back to the visitor as an HTTP cookie.

On a regular basis, the current queue cursor is updated in Redis (INCR queue:cursor). This effectively lets a user in. To show a visitor how many others are in front of them in the queue, we subtract the current queue cursor from the position saved in their JWT.

On subsequent requests, when a JWT is supplied, we verify the signature and extract the position from the JWT. If the current queue cursor is higher than the user's signed position, they will be allowed in.

Security issues

Please see our SECURITY.md for guidance on reporting security-related issues.

compute-starter-kit-javascript-queue's People

Contributors

doramatadora avatar harmony7 avatar integralist avatar jakechampion avatar journie-fastly avatar kailan avatar triblondon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.