GithubHelp home page GithubHelp logo

skygear-demo / web-dinnerpoll Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 1.0 2.5 MB

A single page Skygear web app to demonstrate dinner polling

Home Page: https://skygear-demo.github.io/web-dinnerpoll/

JavaScript 83.40% CSS 15.92% HTML 0.68%
skygear demo web javascript serverless webapp skygear-js skygear-pubsub polling

web-dinnerpoll's Introduction

Web app: Polling for dinner

A web-based serverless polling app using Skygear as cloud database.

External libraries

Guide

This guide will show you how to build this app using the simplest with HTML5 and Skygear JavaScript SDK.

To get a easier start, you can download

Installing JavaScript SDK

We will install Skygear JS SDK via CDN.

<script src="https://code.skygear.io/js/polyfill/latest/polyfill.min.js"></script>
<script src="https://code.skygear.io/js/skygear/latest/skygear.min.js"></script>

Setting up the endpoint

  skygear.config({
    'endPoint': 'https://<appname>.skygeario.com/',
    'apiKey': '<APIKEY HERE>',
  }).then(() => {
    console.log('skygear container is now ready for making API calls.');
    updateAppView();
    if (skygear.auth.currentUser) {
      startAutoReload();
    }
  }, (error) => {
    console.error(error);
  });

Authenticate users

Document for User Authentication Basics

Create a signup page

Sign up a user

function signup (username, password, passwordConfirm) {
  if(checkSignupInfo(username, password, passwordConfirm)) {
    skygear.auth.signupWithUsername(username, password).then((user) => {
      console.log(user); // user object
      swal({
        title: "Welcome",
        text: "Thanks for signing up!",
        type: "success",
        confirmButtonText: "Next"
      });

    }, (error) => {
      swal({
        title: "Error!",
        text: "Hey, "+error.error.message,
        type: "error",
        confirmButtonText: "Okay"
      });
    });
  }
}

Create a login page

Login a user

function login (username, password) {
  skygear.auth.loginWithUsername(username, password).then((user) => {
    console.log(user); // user object
  }, (error) => {
    console.error(error);
    swal({
      title: "Error!",
      text: "Hey, "+error.error.message,
      type: "error",
      confirmButtonText: "Okay"
    });
  })
}

Cast a vote

Cast a vote and save the record in the cloud

You can consider casting a vote as a simple record saving task. Here is the exact breakdown of the steps:

  1. Create a Vote record
  2. Save the record to the cloud database
  3. Get the callback and update the layout

You can imagine that would be API endpoints and ajax request between your app and the server. Alternatively, we often use Backbone.js to manage this kind of data model.

Using the Skygear CloudDB, you will be encapsulated from the underlying layer - no more worry about the requests and API endpoints. You just need to deal with the native objects.

You save the object. You query a list of objects.

Code:

// TODO: Cast a vote here

const vote = new Vote({
  choice: choice
});

skygear.publicDB.save(vote).then(function(){
  reloadChart();
  skygear.pubsub.publish('voted',{choice:vote.choice});
});

How can I retrieve the result?

Query and display the poll result from cloud database

Query from cloud database

Get the result in real-time!

Get real-time polling result without refreshing

Skygear Pubsub

Subscribing to an event

skygear.on('ping', (data) => {
  console.log(data);
});

Publishing an event

skygear.on('ping', (data) => {
  console.log(data);
});

Resources

web-dinnerpoll's People

Contributors

david90 avatar rickmak avatar

Watchers

 avatar  avatar  avatar

Forkers

jeremychan

web-dinnerpoll's Issues

The pubsub is not working on refresh after login

The step to reproduce

  1. BrowserA login as userA, browserB login as userB
  2. Vote by userA is deliver via web socket to userB, and vice versa.
  3. Refresh browserA.
  4. Vote by userA is deliver via web socket to userB, but not vice versa. UserA don't get userB vote via websocket.

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.