Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

Commit

Permalink
Separando logica do firebase para um local separado, adicionando debu…
Browse files Browse the repository at this point in the history
…g nos websockets
  • Loading branch information
eduardopilati committed Apr 30, 2023
1 parent 120a562 commit 483a98c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,7 @@ VITE_PUSHER_HOST=localhost
VITE_PUSHER_PORT=10010
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
VITE_FIREBASE_HABILITADO=
VITE_DEBUG_WEBSOCKETS=

GOOGLE_APPLICATION_CREDENTIALS='/var/www/html/agroarca-equipamentos-credential.json'
83 changes: 83 additions & 0 deletions resources/js/Componentes/Firebase/Firebase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* eslint-disable no-console */
/* eslint-disable prefer-promise-reject-errors */

import { initializeApp } from 'firebase/app'
import { getAnalytics } from 'firebase/analytics'
import { getMessaging, getToken } from 'firebase/messaging'
import { getPerformance } from 'firebase/performance'

let instance

export class Firebase {
firebaseConfig = {
apiKey: 'AIzaSyDK6yPqneJ5TafOA_AySHcCw0wps_F8CPE',
authDomain: 'agroarca-equipamentos.firebaseapp.com',
projectId: 'agroarca-equipamentos',
messagingSenderId: '260002359203',
appId: '1:260002359203:web:8c19a187bf2fe28079dc16',
measurementId: 'G-DHSLGFC76R',
}

vapidKey: string = 'BPlE43kDpMP4nb3ltOOZZRDDxkJA-CKsdim6elA8c5amJmykNZl-_UmxsRGJGe1P3I0R50Qgwyf7Tlaf9ICUcqU'

app
messaging = null
analytics = null
performance = null

constructor() {
this.app = initializeApp(this.firebaseConfig)

let firebaseHabilitado: boolean = import.meta.env.VITE_FIREBASE_HABILITADO
if (!firebaseHabilitado) {
console.warn('Firebase Desabilitado. Ignorando Setup')
return
}

this.loadMessaging()
this.loadAnalytics()
this.loadPerformance()
}

loadMessaging(): void {
try {
this.messaging = getMessaging(this.app)
} catch (error) {
console.error(error)
}
}

loadAnalytics(): void {
try {
this.analytics = getAnalytics(this.app)
} catch (error) {
console.error(error)
}
}

loadPerformance(): void {
try {
this.performance = getPerformance(this.app)
} catch (error) {
console.error(error)
}
}

getMessagingToken() {
if (this.messaging === null) {
return new Promise(() => {
console.warn('Firebase Messaging não carregado, ignorando getToken')
})
}

return getToken(this.messaging, { vapidKey: this.vapidKey })
}
}

export default function getFirebase(): Firebase {
if (!instance) {
instance = new Firebase()
}

return instance
}
8 changes: 8 additions & 0 deletions resources/js/Componentes/Firebase/Firebase.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script setup lang="ts">
import Notificacoes from '../Notificacao/Notificacoes.vue'
</script>

<template>
<Notificacoes />
</template>
24 changes: 4 additions & 20 deletions resources/js/Componentes/Notificacao/Push.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
import { initializeApp } from 'firebase/app'
import { getAnalytics } from 'firebase/analytics'
import { getMessaging, getToken } from 'firebase/messaging'
import axios from 'axios'
import { addDays, isAfter, isBefore, isDate } from 'date-fns'
import getFirebase, { Firebase } from '../Firebase/Firebase'

const firebaseConfig = {
apiKey: 'AIzaSyDK6yPqneJ5TafOA_AySHcCw0wps_F8CPE',
authDomain: 'agroarca-equipamentos.firebaseapp.com',
projectId: 'agroarca-equipamentos',
messagingSenderId: '260002359203',
appId: '1:260002359203:web:8c19a187bf2fe28079dc16',
measurementId: 'G-DHSLGFC76R',
}

const vapidKey = 'BPlE43kDpMP4nb3ltOOZZRDDxkJA-CKsdim6elA8c5amJmykNZl-_UmxsRGJGe1P3I0R50Qgwyf7Tlaf9ICUcqU'
const DIAS_PARA_RENOVAR_TOKEN = 14

let instance

export class Push {
app
messaging
analytics
firebase: Firebase

constructor() {
this.app = initializeApp(firebaseConfig)
this.messaging = getMessaging(this.app)
this.analytics = getAnalytics(this.app)
this.firebase = getFirebase()
}

solicitarPermissao(): Promise<void> {
Expand Down Expand Up @@ -66,7 +50,7 @@ export class Push {
}

criarToken(): void {
getToken(instance.messaging, { vapidKey }).then((token) => {
getPush().firebase.getMessagingToken().then((token) => {
if (token) {
instance.salvarToken(token)
}
Expand Down
7 changes: 7 additions & 0 deletions resources/js/Componentes/Notificacao/Websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function iniciarNotificacoes() {
}

function iniciarPusher() {
window.debugWebsockets = !!import.meta.env.VITE_DEBUG_WEBSOCKETS
window.Echo = new Echo({
...options,
client: new Pusher(options.key, options),
Expand All @@ -56,6 +57,12 @@ function iniciarPusher() {
window.Echo.private(channel)
.listen('.ConversaWebSocket', (e) => conversaWebSocket(e))
.listen('.NotificacaoWebSocket', (e) => notificacaoWebSocket(e))
.listenToAll((a, b) => {
if (window.debugWebsockets) {
/* eslint-disable-next-line no-console */
console.log(a, b)
}
})

window.Pusher = Pusher
}
Expand Down

0 comments on commit 483a98c

Please sign in to comment.