Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 69f2a62

Browse files
committed
add support for firestore
1 parent 7e29176 commit 69f2a62

File tree

5 files changed

+104
-31
lines changed

5 files changed

+104
-31
lines changed

README.md

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
// config/broadcasting
1616

1717
return [
18-
'connections'=> [
18+
'connections' => [
1919
// ...
2020

21-
'firebase'=> [
22-
'driver'=> 'firebase',
23-
'databaseURL'=> env('FB_DB_URL'), // the real time database url
24-
'creds_file'=> env('FB_CREDENTIALS_FILE'), // service account json file
25-
'collection_name'=> env('FB_COLLECTION_NAME'), // ex.notifications
21+
'firebase' => [
22+
'driver' => 'firebase',
23+
'type' => 'rtdb', // rtdb or fsdb
24+
'databaseURL' => env('FB_DB_URL'), // the real time database url
25+
'creds_file' => env('FB_CREDENTIALS_FILE'), // service account json file
26+
'collection_name' => env('FB_COLLECTION_NAME'), // ex.notifications
2627
],
2728
],
2829
];
@@ -33,24 +34,24 @@ return [
3334
- add `BROADCAST_DRIVER=firebase` to `.env`
3435

3536
- atm there no support for [laravel-echo](https://laravel.com/docs/5.8/broadcasting#installing-laravel-echo) "any help is appreciated" but no worries, you still get the same payload as other broadcast drivers.
36-
37-
however you can check the [firebase api docs](https://firebase.google.com/docs/database/web/start) or [vuefire](https://github.com/vuejs/vuefire) if you are using `vue`, on how to listen for changes and update your app users accordingly.
3837

38+
however you can check the [firebase api docs](https://firebase.google.com/docs/database/web/start) or [vuefire](https://github.com/vuejs/vuefire) if you are using `vue`, on how to listen for changes and update your app users accordingly.
3939

4040
#### Notification Data Sample
4141
```json
4242
{
43-
"notifications" : {
44-
"-LkgtAVVw0Ztwyjayd9n" : {
45-
"channels" : [ "private-App.User.091b0f7e-805b-4aab-8c99-445039157783" ],
46-
"data" : {
47-
"body" : "some body",
48-
"id" : "d54c44a2-8a42-43a4-bae0-e2b159d1533b",
49-
"title" : "some title",
50-
"type" : "App\\Notifications\\AlertUser"
51-
},
52-
"event" : "Illuminate\\Notifications\\Events\\BroadcastNotificationCreated"
53-
},
54-
}
43+
"notifications" : {
44+
"-LkgtAVVw0Ztwyjayd9n" : {
45+
"channels" : [ "private-App.User.091b0f7e-805b-4aab-8c99-445039157783" ],
46+
"data" : {
47+
"body" : "some body",
48+
"id" : "d54c44a2-8a42-43a4-bae0-e2b159d1533b",
49+
"title" : "some title",
50+
"type" : "App\\Notifications\\AlertUser"
51+
},
52+
"event" : "Illuminate\\Notifications\\Events\\BroadcastNotificationCreated",
53+
"timestamp": 1564183089538
54+
}
55+
}
5556
}
5657
```

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
"illuminate/support": "5.5 - 5.8",
2121
"kreait/firebase-php": "*"
2222
},
23+
"suggest": {
24+
"morrislaptop/firestore-php": "Required to use with firestore db."
25+
},
2326
"autoload": {
2427
"psr-4": {
2528
"ctf0\\Firebase\\": "src"
@@ -38,7 +41,7 @@
3841
},
3942
"scripts": {
4043
"post-package-install": [
41-
"@php artisan vendor:publish --provider=\"ctf0\\Firebase\\FireBaseBroadcastProvider\""
44+
"@php artisan vendor:publish --provider = \"ctf0\\Firebase\\FireBaseBroadcastProvider\""
4245
]
4346
}
4447
}

src/FireBaseBroadcaster.php renamed to src/Broadcasters/FSDB.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
22

3-
namespace ctf0\Firebase;
3+
namespace ctf0\Firebase\Broadcasters;
44

5-
use Illuminate\Support\Arr;
6-
use Kreait\Firebase\Factory;
5+
use Illuminate\Support\Str;
76
use Kreait\Firebase\ServiceAccount;
7+
use Morrislaptop\Firestore\Factory;
88
use Kreait\Firebase\Exception\ApiException;
99
use Illuminate\Broadcasting\BroadcastException;
1010
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
1111

12-
class FireBaseBroadcaster extends Broadcaster
12+
class FSDB extends Broadcaster
1313
{
1414
protected $db;
1515
protected $config;
@@ -21,12 +21,11 @@ public function __construct()
2121
{
2222
$this->config = config('broadcasting.connections.firebase');
2323

24-
$sr_account = ServiceAccount::fromJsonFile(base_path(Arr::get($this->config, 'creds_file')));
24+
$sr_account = ServiceAccount::fromJsonFile(base_path($this->config['creds_file']));
2525

2626
$this->db = (new Factory())
2727
->withServiceAccount($sr_account)
28-
->withDatabaseUri(Arr::get($this->config, 'databaseURL'))
29-
->create();
28+
->createFirestore();
3029
}
3130

3231
/**
@@ -48,10 +47,12 @@ public function validAuthenticationResponse($request, $result)
4847
*/
4948
public function broadcast(array $channels, $event, array $payload = [])
5049
{
51-
$db = $this->db->getDatabase();
50+
$db = $this->db;
5251

5352
try {
54-
$db->getReference(Arr::get($this->config, 'collection_name'))->push([
53+
$coll = $db->collection($this->config['collection_name']);
54+
$doc = $coll->document(md5(Str::uuid()));
55+
$doc->set([
5556
'timestamp' => round(now()->valueOf()), // return date == to js Date.now()
5657
'channels' => $this->formatChannels($channels),
5758
'data' => $payload,

src/Broadcasters/RTDB.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace ctf0\Firebase\Broadcasters;
4+
5+
use Kreait\Firebase\Factory;
6+
use Kreait\Firebase\ServiceAccount;
7+
use Kreait\Firebase\Exception\ApiException;
8+
use Illuminate\Broadcasting\BroadcastException;
9+
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
10+
11+
class RTDB extends Broadcaster
12+
{
13+
protected $db;
14+
protected $config;
15+
16+
/**
17+
* Create a new broadcaster instance.
18+
*
19+
* @param mixed $config
20+
*/
21+
public function __construct($config)
22+
{
23+
$sr_account = ServiceAccount::fromJsonFile(base_path($config['creds_file']));
24+
$this->config = $config;
25+
$this->db = (new Factory())
26+
->withServiceAccount($sr_account)
27+
->withDatabaseUri($config['databaseURL'])
28+
->create();
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function auth($request)
35+
{
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function validAuthenticationResponse($request, $result)
42+
{
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function broadcast(array $channels, $event, array $payload = [])
49+
{
50+
$db = $this->db->getDatabase();
51+
52+
try {
53+
$db->getReference($this->config['collection_name'])
54+
->push([
55+
'timestamp' => round(now()->valueOf()), // return date == to js Date.now()
56+
'channels' => $this->formatChannels($channels),
57+
'data' => $payload,
58+
'event' => $event,
59+
]);
60+
} catch (ApiException $e) {
61+
throw new BroadcastException($e);
62+
}
63+
}
64+
}

src/FireBaseBroadcastProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace ctf0\Firebase;
44

5+
use ctf0\Firebase\Broadcasters\FSDB;
6+
use ctf0\Firebase\Broadcasters\RTDB;
57
use Illuminate\Support\ServiceProvider;
68
use Illuminate\Broadcasting\BroadcastManager;
79

@@ -13,7 +15,9 @@ class FireBaseBroadcastProvider extends ServiceProvider
1315
public function boot()
1416
{
1517
app(BroadcastManager::class)->extend('firebase', function ($app) {
16-
return new FireBaseBroadcaster();
18+
$config = config('broadcasting.connections.firebase');
19+
20+
return $config['type'] == 'rtdb' ? new RTDB($config) : new FSDB($config);
1721
});
1822
}
1923

0 commit comments

Comments
 (0)