GithubHelp home page GithubHelp logo

server-in-client's Introduction

This is a POC project that dmonstrates how you can create an app which is fully serverless. And by serverless, we mean - no server at all! Instead, we've created the full server architecture in the browser.

architecture

Watermark app

The watermark up is a good demonstartion of the server in client abilities. The app requires:

  • Highly available server
  • Work offline
  • Consistent Data
  • Upload / Download Files
  • Background operations

You can see this full functioning app here

How to configure your server?

Create routes

Routes use the same notion as express.js. You should configure them in routes.js

  app.post("/data/*", (req, res) => {
   ...
  });


  app.get("/files/*", (req, res) => {
   ...
  });

for example:

app.get("/data/*", (req, res) => {
  let path = getDBPathFromUrl(req.url);
  res.send(
    readtheDatafromIndexedDb(
      "data",
      path,
    )
      .then(response => { return response; })
  )
});

Background tasks

The server is a single-threaded based with event loop. All handlers should use async calls for IO. However, if you still need to run long task you can do this by defining workers to handle tasks that are consumed from a queue

Start consumers

for(var i=0; i < 5; i++){
  new Worker('watermarkingWorker.js');
}

Producer code example

hustle.Queue.put({path: path}, {
            tube: 'watermarking',
            ttr: 3,
});

Consumer code example

var consumer = new hustle.Queue.Consumer(fn, {
      tube: 'watermarking',
      delay: 100,
});

fn = () => {...}

Serving

Deploy react-app to Github Pages

  1. Install GitHub Pages - npm install gh-pages --save-dev
  2. Add properties to package.json file - "http://{username}.github.io/{repo-name}" and in the existing scripts property we to need to add predeploy and deploy.
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
}

3.deploy - npm run deploy

Caching

Other resources that don't match any route, will be fetched from the server, and cached for future usage.

caches.match(req).then(match => {
  return fetch(req).then(response => {
    return caches.open("resources").then(cache => cache.put(req, response.clone())).then(()=>
      response
    )
  }).catch(()=> match);

Front end

Front end code is fully decoupled from server implementation. fetch and resource fetching should be working natively.

Fetch

 handleDeleteFile = (path) => {
      fetch(path, { method: 'DELETE' })
        .then(this.fetchImageListFromServer())
  }

Resources

<img src="/files/withoutWatermark/..." />

server-in-client's People

Contributors

akiriati avatar

Stargazers

 avatar

Watchers

 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.