SlideShare a Scribd company logo
Polymer & Firebase
ハンズオン
おのうえ(@_likr)
2015/10/11 GDG DevFest Kobe Firebaseハンズオン勉強会
自己紹介
• おのうえ(@_likr)
• ng-kyoto、GDG Kobeスタッフ
• 大学院でWebベース可視化システムの研究・開発
今日の目的
• Firebase + Polymerでお手軽
Webアプリ開発を体験してみる
1. 概要
2. 準備
3. Firebase
4. Polymer
5. firebase-element
6. firebase-element + Google API
7. もくもく会
1. 概要
2. 準備
3. Firebase
4. Polymer
5. firebase-element
6. firebase-element + Google API
7. もくもく会
Firebase
• BaaS (Backend as a Service)
• サーバー機能(Web + App + DB)を提供
• サーバー側プログラミング不要
• クライアントはWeb、iOS、Android、RESTが対応
• Google Cloud Platformの一部
Firebaseの特徴
• Realtime Database
• 透過的な保存と更新
• User Authentication
• 様々なProviderによるログイン、ログアウト
• Static Hosting
• HTTP HeaderやRedirectの設定
ライブラリ
• AngularFire (AngularJS)
• EmberFire (Ember)
• ReactFire (ReactJS)
• PolymerElement (Polymer)
• Ionic
Polymer
• Google主導のオープンソースフレームワーク

https://www.polymer-project.org/
• WebComponents + α
• Webアプリ内の再利用性の高い機能を

コンポーネント化し利用しやすくする仕組み
PolymerElementを使う
$ bower install hoge-element
<!DOCTYPE html>
<html>
<head>
<script
src=“bower_components/webcomponentsjs/webcomponents-lite.js”>
</script>
<link rel=“import”
href=“bower_components/hoge-element/hoge-element.html”>
</head>
<body>
<hoge-element></hoge-element>
</body>
</html>
使用するElementをbowerでインストール
タグとして使用
Custom Elements, HTML ImportsのPolyfillをロード
Elementをインポート
Element Catalog
• 公式が配布するPolymerElement集
• https://elements.polymer-project.org/
• マテリアルデザイン、Google API、アニメーショ
ン、EC用フォーム、オフライン…
サンプル
<!DOCTYPE html>
<html>
<head>
<script src="bower_components/webcomponentsjs/webcomponents-lite.js">
</script>
<link rel="import" href="bower_components/google-map/google-map.html">
<style>
google-map {
height: 600px;
}
</style>
</head>
<body>
<google-map latitude="34.682933" longitude="135.506919" fit-to-markers>
<google-map-marker latitude="34.682933" longitude="135.506919">
</google-map-marker>
</google-map>
</body>
</html>
index.html
google-mapをインポート
Google Mapを表示
マーカーを作成
GDG DevFest Kobe Firebaseハンズオン勉強会
PolymerElementを作る
<link rel="import" href="bower_components/polymer/polymer.html">
<dom-module id="my-chart">
<link rel="stylesheet" href="bower_components/nvd3/build/nv.d3.css">
<style>
.view {
width: 600px;
height: 600px;
}
</style>
<template>
<div class="view"><svg id="svg"></svg></div>
</template>
<script src="bower_components/d3/d3.js"></script>
<script src="bower_components/nvd3/build/nv.d3.js"></script>
<script src="my-chart.js"></script>
</dom-module>
my-chart.html
polymerをインポート
利用側に挿入されるHTML要素を定義
my-chart要素を定義
Polymer({
is: 'my-chart',
ready: function () {
nv.addGraph(() => {
const chart = nv.models.discreteBarChart()
.x((d) => d.label)
.y((d) => d.value)
.staggerLabels(true)
.tooltips(false)
.showValues(true);
d3.select(this.$.svg)
.datum(exampleData())
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
}
});
my-chart.js
my-chartの振る舞いを定義
ライフサイクルメソッド
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script
src=“bower_components/webcomponentsjs/webcomponents-lite.js">
</script>
<link rel="import" href="my-chart.html">
</head>
<body>
<my-chart></my-chart>
</body>
</html>
index.html
GDG DevFest Kobe Firebaseハンズオン勉強会
1. 概要
2. 準備
3. Firebase
4. Polymer
5. firebase-element
6. firebase-element + Google API
7. もくもく会
準備
• Googleアカウントをつくる

https://accounts.google.com/signup
• Firebaseアカウントをつくる

https://www.firebase.com/
• Node.jsをインストールする

https://nodejs.org/
• Chrome Dev Editorをインストールする

https://chrome.google.com/webstore/category/apps
1. 概要
2. 準備
3. Firebase
4. Polymer
5. firebase-element
6. firebase-element + Google API
7. もくもく会
Firebase
• リアルタイムデータベースを触ってみる
• デプロイしてみる
プロジェクト作成
Chrome Dev EditorでBlank projectを作成
bower.json
{
"name": "FirebaseHandson01",
"description": "",
"homepage": "",
"keywords": [],
"author": "",
"private": true,
"dependencies": {
"firebase": "~2.3.1"
}
}
作成後、プロジェクトを右クリックして「bower install」
フロントエンドのパッケージ管理ツールbowerを使う

(Chrome Dev Editorに標準搭載)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div>
<form id="message-form">
<input id="message-input" type="text">
<button type="submit">Update</button>
</form>
</div>
<div>
<p id="date"></p>
<p id="message"></p>
</div>
<script src="bower_components/firebase/firebase.js"></script>
<script src="main.js"></script>
</body>
</html>
main.js
var firebaseURL = 'https://<your-app-id>.firebaseio.com';
var ref = new Firebase(firebaseURL + '/message');
ref.on('value', function (snapshot) {
var message = snapshot.val();
if (message) {
document.getElementById('date').innerHTML = new Date(message.date);
document.getElementById('message').innerHTML = message.message;
}
});
document.getElementById('message-form')

.addEventListener('submit', function (event) {
event.preventDefault();
ref.set({
message: document.getElementById('message-input').value,
date: Firebase.ServerValue.TIMESTAMP
});
});
main.js 各自のアプリケーションIDに書き換える
GDG DevFest Kobe Firebaseハンズオン勉強会
GDG DevFest Kobe Firebaseハンズオン勉強会
デプロイ
• コマンドラインツールのインストール

$ npm install -g firebase-tools
• 設定ファイルの初期化

$ firebase init
• デプロイ

$ firebase deploy
• ブラウザで開く

$ firebase open
1. 概要
2. 準備
3. Firebase
4. Polymer
5. firebase-element
6. firebase-element + Google API
7. もくもく会
Polymer
• コードラボやります
• Build Google Maps Using Web Components & No
Code!

http://www.code-labs.io/codelabs/polymer-maps/
index.html
1. 概要
2. 準備
3. Firebase
4. Polymer
5. firebase-element
6. firebase-element + Google API
7. もくもく会
firebase-element
• コードラボやります
• Interacting with data using the <firebase-element>

http://www.code-labs.io/codelabs/polymer-firebase/
index.html
訂正
app.onFirebaseLogin = function(event) {
this.ref = new Firebase(this.firebaseURL + '/user/' +
event.detail.user.uid);
this.ref.on('value', function(snapshot) {
this.updateItems(snapshot);
});
};
app.onFirebaseLogin = function(event) {
this.ref = new Firebase(this.firebaseURL + '/user/' +
event.detail.user.uid);
this.ref.on('value', function(snapshot) {
app.updateItems(snapshot);
});
};
https://github.com/googlecodelabs/polymer-firebase/issues/1
http://www.code-labs.io/codelabs/polymer-firebase/index.html#4
1. 概要
2. 準備
3. Firebase
4. Polymer
5. firebase-element
6. firebase-element + Google API
7. もくもく会
firebase-element + Google API
• PolymerFirebaseCodelabの続きから
• FirebaseのOAuthで取得したaccessTokenで

Google APIにアクセスしてみる
• Google Driveに保存する機能を追加する
ログアウトボタン
<firebase-auth
id="auth"
auto-login
redirect
location="[[firebaseURL]]"
provider="[[firebaseProvider]]"
on-error="onFirebaseError"
on-login="onFirebaseLogin"
on-logout="onFirebaseLogout">
index.html
app.onFirebaseLogout = function (event) {
};
app.login = function (event) {
this.$.auth.login();
};
app.logout = function (event) {
this.$.auth.logout();
};
main.js
<link rel="import" href="bower_components/paper-button/paper-button.html">
paper-buttonのimportを追加
<paper-button on-click="login">Login</paper-button>
<paper-button on-click="logout">Logout</paper-button>
paper-buttonを追加
<paper-input
value="{{newItemValue}}"
placeholder="Enter you item here…"
disabled="[[!loggedIn]]">
</paper-input>
loginしていないと入力できないようにする
app.loggedIn = false;
app.onFirebaseLogin = function (event) {
this.loggedIn = true;
this.ref = new Firebase(this.firebaseURL + '/user/'
+ event.detail.user.uid);
this.ref.on('value', function (snapshot) {
this.updateItems(snapshot);
}.bind(this));
};
app.onFirebaseLogout = function (event) {
this.loggedIn = false;
this.items = [];
};
Drive APIの有効化
Google Developers ConsoleからDrive APIを有効化する
scopeを追加
app.firebaseParams = {
scope: "https://www.googleapis.com/auth/drive"
};
<firebase-auth
id="auth"
redirect
location="[[firebaseURL]]"
provider="[[firebaseProvider]]"
params="[[firebaseParams]]"
on-error="onFirebaseError"
on-login="onFirebaseLogin"
on-logout="onFirebaseLogout">
</firebase-auth>
iron-ajax
<link rel="import" href="bower_components/iron-ajax/iron-ajax.html">
<iron-ajax
id="uploadFile"
method="POST"
params="[[requestParams]]"
body="[[requestBody]]"
url="https://www.googleapis.com/upload/drive/v2/files">
iron-ajaxを使ってAPIにリクエストを行う
accessTokenの取得
app.onFirebaseLogin = function (event) {
this.accessToken = event.detail.user.google.accessToken;
this.loggedIn = true;
this.ref = new Firebase(this.firebaseURL + '/user/'
+ event.detail.user.uid);
this.ref.on('value', function (snapshot) {
this.updateItems(snapshot);
}.bind(this));
};
アップロードボタン
<paper-button on-click="uploadFile">Upload</paper-icon-button>
app.uploadFile = function (event) {
this.requestParams = {
access_token: this.accessToken,
uploadType: 'media'
};
this.requestBody = this.items.map(function (item) {
return item.text;
}).join('nn');
this.$.uploadFile.generateRequest();
};
GDG DevFest Kobe Firebaseハンズオン勉強会
ポイント
• FirebaseのOAuth時にscopeを指定する
• Firebaseの認証情報からaccessTokenを取得する
• REST APIにアクセスする
1. 概要
2. 準備
3. Firebase
4. Polymer
5. firebase-element
6. firebase-element + Google API
7. もくもく会
参考情報
• Polymer Codelabs (Polymer Summit2015)

http://www.code-labs.io/polymer-summit
• Developer Docs - Firebase

https://www.firebase.com/docs/

More Related Content

PDF
Wordpress案件にgkeを採用してみた(短縮版)
PDF
appengine ja night #25 Google App Engine for PHP
PPTX
Myfirst buildpack session_mgmt_20161201
PDF
Cloud Foundryで学ぶ、PaaSのしくみ講座
KEY
1Day works shop
PDF
DDDとクリーンアーキテクチャでサーバーアプリケーションを作っている話
PDF
パフォーマンス計測Ciサービスを作って得た知見を共有したい
PDF
Yahoo! JAPANのコンテンツプラットフォームを支えるSpring Cloud Streamによるマイクロサービスアーキテクチャ #jsug #sf_52
Wordpress案件にgkeを採用してみた(短縮版)
appengine ja night #25 Google App Engine for PHP
Myfirst buildpack session_mgmt_20161201
Cloud Foundryで学ぶ、PaaSのしくみ講座
1Day works shop
DDDとクリーンアーキテクチャでサーバーアプリケーションを作っている話
パフォーマンス計測Ciサービスを作って得た知見を共有したい
Yahoo! JAPANのコンテンツプラットフォームを支えるSpring Cloud Streamによるマイクロサービスアーキテクチャ #jsug #sf_52

What's hot (12)

PDF
PaaS / Cloud Foundry makes you happy
PDF
Application development with c#, .net 6, blazor web assembly, asp.net web api...
PDF
Cloud FoundryでDockerも.NETも。新しいDiegoの仕組み入門
PDF
Google Container Engine (GKE) & Kubernetes のアーキテクチャ解説
PDF
KubernetesでPHPを動かした話
PPTX
はじめての Cloud Foundry: .NET アプリケーションのはじめ方
PDF
Docker PaaSとしての OpenShift, Deis, Flynn比較
PDF
『コンテナ疲れ』と戦う、k8s・PaaS・Serverlessの活用法
PDF
はじめてのCF buildpack
PDF
VSCodeで始めるAzure Static Web Apps開発
PDF
Cloud functions for Firebase
PDF
フィードフォースと AWS と私
PaaS / Cloud Foundry makes you happy
Application development with c#, .net 6, blazor web assembly, asp.net web api...
Cloud FoundryでDockerも.NETも。新しいDiegoの仕組み入門
Google Container Engine (GKE) & Kubernetes のアーキテクチャ解説
KubernetesでPHPを動かした話
はじめての Cloud Foundry: .NET アプリケーションのはじめ方
Docker PaaSとしての OpenShift, Deis, Flynn比較
『コンテナ疲れ』と戦う、k8s・PaaS・Serverlessの活用法
はじめてのCF buildpack
VSCodeで始めるAzure Static Web Apps開発
Cloud functions for Firebase
フィードフォースと AWS と私
Ad

Viewers also liked (20)

PDF
Polymerやってみた
PDF
AngularJSでデータビジュアライゼーションがしたい
PDF
Angular 2のRenderer
PDF
アニメーション(のためのパフォーマンス)の基礎知識
PDF
GPGPU Seminar (PyCUDA)
PPTX
PyCUDAの紹介
PDF
Introduce build in shrinker
KEY
GPGPU deいろんな問題解いてみた
PPTX
x86x64 SSE4.2 POPCNT
PDF
AVX2時代の正規表現マッチング 〜半群でぐんぐん!〜
PDF
Gradle PluginとCIと俺
PDF
Popcntによるハミング距離計算
PDF
GPUが100倍速いという神話をぶち殺せたらいいな ver.2013
PPTX
Jetson TK1でSemi-Global Matching
PPT
Rsa暗号で彼女が出来るらしい
PDF
CUDA 6の話@関西GPGPU勉強会#5
PPTX
教育機関でのJetsonの活用の可能性
KEY
PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編
PDF
GPGPU Education at Nagaoka University of Technology: A Trial Run
PDF
Cuda fortranの利便性を高めるfortran言語の機能
Polymerやってみた
AngularJSでデータビジュアライゼーションがしたい
Angular 2のRenderer
アニメーション(のためのパフォーマンス)の基礎知識
GPGPU Seminar (PyCUDA)
PyCUDAの紹介
Introduce build in shrinker
GPGPU deいろんな問題解いてみた
x86x64 SSE4.2 POPCNT
AVX2時代の正規表現マッチング 〜半群でぐんぐん!〜
Gradle PluginとCIと俺
Popcntによるハミング距離計算
GPUが100倍速いという神話をぶち殺せたらいいな ver.2013
Jetson TK1でSemi-Global Matching
Rsa暗号で彼女が出来るらしい
CUDA 6の話@関西GPGPU勉強会#5
教育機関でのJetsonの活用の可能性
PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編
GPGPU Education at Nagaoka University of Technology: A Trial Run
Cuda fortranの利便性を高めるfortran言語の機能
Ad

Similar to GDG DevFest Kobe Firebaseハンズオン勉強会 (17)

PPTX
JavaScriptから利用するFirebase
PPTX
Firebase hands on in Matsuyama
PDF
AngularFireで楽々バックエンド
PDF
Vue.js + firebase 実案件で使ってみた
PPTX
FirebaseではじめるサーバレスSPA開発
PDF
Firebase hands on_#1
PDF
Firebase Extensions はじめの一歩
PDF
Firebase初心者がwebチャットアプリを作ってみた
PDF
Firebaseを使ってアプリを作ってみた
PDF
Firebase によるリアルタイム モバイル開発 @gcpug 福岡
PDF
Firestoreを勉強してみた
PDF
ruby、sinatraで作るfacebookアプリ
PPT
Inside mobage platform
PPTX
Android 開発を加速するオープンソースライブラリ
PPTX
2C向けグローバルプロダクトでのFirebaseの使い方_Glasp_05/21/2024
PPTX
ログインの全て
PDF
Firebase Realtime Database を C# から利用する
JavaScriptから利用するFirebase
Firebase hands on in Matsuyama
AngularFireで楽々バックエンド
Vue.js + firebase 実案件で使ってみた
FirebaseではじめるサーバレスSPA開発
Firebase hands on_#1
Firebase Extensions はじめの一歩
Firebase初心者がwebチャットアプリを作ってみた
Firebaseを使ってアプリを作ってみた
Firebase によるリアルタイム モバイル開発 @gcpug 福岡
Firestoreを勉強してみた
ruby、sinatraで作るfacebookアプリ
Inside mobage platform
Android 開発を加速するオープンソースライブラリ
2C向けグローバルプロダクトでのFirebaseの使い方_Glasp_05/21/2024
ログインの全て
Firebase Realtime Database を C# から利用する

More from Yosuke Onoue (10)

PDF
asm.jsとWebAssemblyって実際なんなの?
PDF
AngularJSとD3.jsによるインタラクティブデータビジュアライゼーション
PDF
AngularJSでの非同期処理の話
PDF
社会的決定とAHP
PDF
Anaconda & NumbaPro 使ってみた
PDF
PythonistaがOCamlを実用する方法
KEY
What's New In Python 3.3をざっと眺める
KEY
PyOpenCLによるGPGPU入門
PPTX
数理最適化とPython
PPTX
201010ksmap
asm.jsとWebAssemblyって実際なんなの?
AngularJSとD3.jsによるインタラクティブデータビジュアライゼーション
AngularJSでの非同期処理の話
社会的決定とAHP
Anaconda & NumbaPro 使ってみた
PythonistaがOCamlを実用する方法
What's New In Python 3.3をざっと眺める
PyOpenCLによるGPGPU入門
数理最適化とPython
201010ksmap

GDG DevFest Kobe Firebaseハンズオン勉強会