51
2015/04/11 Ian Lewis Developer Advocate @Google Firebaseによるリアルタイムモバ イル開発 2015/04/11

Firebase によるリアルタイム モバイル開発 @gcpug 福岡

Embed Size (px)

Citation preview

2015/04/11

Ian LewisDeveloper Advocate @Google

Firebaseによるリアルタイムモバイル開発2015/04/11

2015/04/11

Ian LewisDeveloper AdvocateGoogle Cloud Platform

google.com/+IanLewis-hoge@IanMLewis

2015/04/11

Getting Started

Agenda

— なんでモバイルにする必要があるのか

— バックエンドサーバーの構築は?

— なんでFirebaseを使うと便利か

— これから

2015/04/11

なんでモバイルにする必要があるのか

2015/04/11

なんでモバイルにする必要があるのか

マルチでバイス

2015/04/11

なんでモバイルにする必要があるのか

本当にマルチデバイス

Wear TV Auto

2015/04/11

バックエンドの構築は?

2015/04/11

バックエンドの構築は困難

2015/04/11

バックエンドの構築は?

モバイル開発は違う

— 様々な画面サイズ

— 個人で利用するデバイス

— ネットワークの信頼性が低い

2015/04/11

バックエンドの構築は?

モバイル開発はチャレンジング

— 多数のプラットフォームで動く

— ユーザーはリアルタイム性を期待している

— 常に繋がっているデバイスは、アプリの状態が分散される

— 多数のデバイスは巨大なスケールが必要

2015/04/11

なんでFirebaseを使うと便利か

2015/04/11

なんでFirebaseを使うと便利か

Firebase: リアルタイムアプリケーションプラットフォーム

リアルタイムDB ユーザー管理 ファイル

ホスティング

2015/04/11

なんでFirebaseを使うと便利か

リアルタイムデータベース

● NoSQL, JSON データベース

● ミリ秒単位で更新をプッシュ

● デバイスから直接のアクセスを

可能とするセキュリティモデル

● データとURLのマッピング

01 / 23

2015/04/11

なんでFirebaseを使うと便利か

計算性能のシフト

Smart Client

2015/04/11

なんでFirebaseを使うと便利か

両方向に同期

Firebase

Sync

Smart Client

2015/04/11

なんでFirebaseを使うと便利か

コラボレーション

Smart Clients

Sync

Firebase

2015/04/11

なんでFirebaseを使うと便利か

オフライン

Smart Clients

Sync

Firebase

2015/04/11

なんでFirebaseを使うと便利か

ユーザー管理

以下の方法で簡単にログイン:

● メアドとパスワード

● oAuthプロバイダー ○ Google○ Facebook○ Twitter○ Github

● カスタム認証トークン

2015/04/11

認証とオンライン状態

認証プロバイダー

2015/04/11

なんでFirebaseを使うと便利か

ファイルホースティング

一つのコマンドでホスティング:

● カスタムドメイン

● セキュア (SSL)

● 高速 (CDN)

2015/04/11

簡単なコーディング

Legal StuffCopyright (c) 2014 Firebase

Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.

2015/04/11

始めに

2015/04/11

npm install -g firebase-tools

2015/04/11

firebase init

2015/04/11

簡単なコーディング

ファイルホスティング

{ "firebase": "myfirebase", "public": "app", "ignore": [ "firebase.json", "**/.*", "**/node_modules/**" ]}

Java

2015/04/11

firebase deploy

2015/04/11

認証とオンライン状態

2015/04/11

Create a new Google Apphttps://console.developers.google.com/project

1

Configure OAuth 2.0 Callbackhttps://auth.firebase.com/v2/<YOUR-FIREBASE>/auth/twitter/callback

2

Display Google Login Pop-Up3

var ref = new Firebase("https://<YOUR-FIREBASE>.firebaseio.com/");

ref.authWithOAuthPopup("google", function(error, authData) { ... });

簡単なコーディング

Example: Google認証を使う

2015/04/11

簡単なコーディング

Example: Google認証を使う

private void getGoogleOAuthTokenAndLogin() {

AsyncTask<Void, Void, String> task = new AsyncTask<>() {

@Override

protected String doInBackground(Void... params) {

String scope = String.format("oauth2:%s", Scopes.PLUS_LOGIN);

String token = GoogleAuthUtil.getToken(this,

Plus.AccountApi.getAccountName(mGoogleApiClient), scope);

return token;

}

...

Java

2015/04/11

簡単なコーディング

Example: Google認証を使うSubtitleJavaJava

...

@Override

protected void onPostExecute(String token) {

Intent intentWithToken = new Intent(this,

OfficeMoverActivity.class);

intentWithToken.putExtra(AUTH_TOKEN_EXTRA, token);

startActivity(intentWithToken);

}

}

2015/04/11

簡単なコーディング

Example: Google認証を使うSubtitleJava

mFirebaseRef.authWithOAuthToken("google", authToken, new

Firebase.AuthResultHandler() {

@Override

public void onAuthenticated(AuthData authData) {

Log.v(TAG, "Auth success");

}

@Override

public void onAuthenticationError(FirebaseError firebaseError) {

Toast.makeText(getApplicationContext(),

"Auth failed. Please try again",

Toast.LENGTH_SHORT).show();

}

});

Java

2015/04/11

簡単なコーディング

Example: オンライン状態

var amOnline = new Firebase('https://<YOUR-FIREBASE>.firebaseio.com/.

info/connected');

var presenceRef = new Firebase('https://<YOUR-FIREBASE>.firebaseio.

com/presence/'

+ userid);

amOnline.on('value', function(value) {

if (value.val() === true) {

// Connected (or reconnected)!

presenceRef.onDisconnect().set(false); // one off, has to be reregistered

presenceRef.set(true);

}

});

Java

2015/04/11

セキュリティ

2015/04/11

簡単なコーディング

Example: セキュリティ

{ ... "rules": { ".read": true, ".write": true }}

Java

2015/04/11

簡単なコーディング

Example: セキュリティ

"foo": { ... ".validate": "newData.isString() && newData.val().length < 100"}

Java

2015/04/11

簡単なコーディング

Example: セキュリティ

"users": { "$uid": { ".read": "auth != null && auth.uid == $uid" }}

Java

2015/04/1101 / 23

読み取りと書き込み

2015/04/11

簡単なコーディング

読み取りと書き込みSubtitleJavaScript

Firebase ref = new Firebase("https://<your-firebase>.firebaseio.com");

ref.child("background").setValue("grid");

ref.child("background").addValueEventListener(new ValueEventListener() {

@Override

public void onDataChange(DataSnapshot dataSnapshot) {

System.out.println(dataSnapshot.getValue()); // prints “grid”

}

@Override public void onCancelled(FirebaseError error) { }

});

2015/04/11

簡単なコーディング

読み取りと書き込みSubtitle Java

Firebase ref = new Firebase("https://<your-firebase>.firebaseio.com");

ref.child("background").setValue("grid");

ref.child("background").addValueEventListener(new ValueEventListener()

{

@Override

public void onDataChange(DataSnapshot dataSnapshot) {

System.out.println(dataSnapshot.getValue()); // prints “Grid”

}

@Override public void onCancelled(FirebaseError error) { }

});

2015/04/11

簡単なコーディング

読み取りと書き込みSubtitle Java

Firebase ref = new Firebase("https://<your-firebase>.firebaseio.com");

ref.child("background").setValue("grid");

ref.child("background").addValueEventListener(new ValueEventListener()

{

@Override

public void onDataChange(DataSnapshot dataSnapshot) {

System.out.println(dataSnapshot.getValue()); // prints “grid”

}

...

});

2015/04/11

簡単なコーディング

読み取りと書き込みSubtitle JSON

{

"background": ”grid”

}

2015/04/11

簡単なコーディング

読み取りと書き込みSubtitleJavaScript

OfficeThing newThing = new OfficeThing();

newThing.setName(“Don Quixote”);

ref.child("object").setValue(newThing);

/* This will replace the data at “object” with

"object" : {

"name" : "Don Quixote",

}

*/

2015/04/11

簡単なコーディング

読み取りと書き込みSubtitleJavaScript

Map<String, Object> name = new HashMap<String, Object>();

name.put(“name”, “Don Quixote”);

ref.child("object").update_children(name);

// You could also setValue() with the entire object

2015/04/11

Demo

2015/04/1101 / 23

これから

2015/04/11

これから

リリースする予定

● New! より強力なクエリー詳細はFirebaseブログまで

● 全プラットフォームのオフライン対応● よりいいユーザー管理とAnalytics● Google Cloud Platformとの連携

2015/04/11

Google Cloud Platformと連携する

Triggers

Firebase AppEngine BigQuery

Mobile Device

Sync

REST REST

これから

2015/04/11

これから

Google Cloud Platformと連携

“.trigger": {

“criteria": “data == ‘hello world’",

“method": “'POST'",

“target": “'https://my.externalservice.com/endpoint'",

“body": {

“text": “’someone said hello!’",

}

}

JSON

2015/04/11

www.firebase.com

2015/04/11

Getting Started

firebase.com/tutorial

2015/04/11

Thanks !Ian Lewis

g+ — plus.google.com/+IanLewis-hogetwitter — @IanMLewiswww — www.ianlewis.orggithub — github.com/IanLewis