Flutter firebase connect to emulator from real device - firebase

Hi I am currently using firebase's local emulator and flutter. However, I am using a real device and not a simulator therefore I do not know how to connect to my laptops localhost. I am currently using this code
// [Firestore | localhost:8080]
FirebaseFirestore.instance.settings = const Settings(
host: "localhost:8080",
sslEnabled: false,
persistenceEnabled: false,
);
// [Authentication | localhost:9099]
await FirebaseAuth.instance.useEmulator("http://localhost:9099");
FirebaseFunctions.instance.useFunctionsEmulator(
origin: "http://localhost:5001"
);
// [Storage | localhost:9199]
await FirebaseStorage.instance.useEmulator(
host: "localhost",
port: 9199,
);

Ok I fixed the problem by these two steps:
firebase.json:
{
...
"emulators": {
"auth": {
"host": "0.0.0.0", <--- Adding host
"port": 9099
},
"functions": {
"host": "0.0.0.0",
"port": 5001
},
"firestore": {
"host": "0.0.0.0",
"port": 8080
},
"storage": {
"host": "0.0.0.0",
"port": 9199
},
"ui": {
"enabled": true
}
},
...
}
flutter main.dart:
const String localIp = "You local ip goes here";
FirebaseFirestore.instance.settings = const Settings(
host: localIp+":8080",
sslEnabled: false,
persistenceEnabled: false,
);
await FirebaseAuth.instance.useEmulator("http://"+localIp+":9099");
FirebaseFunctions.instance.useFunctionsEmulator(
origin: "http://"+localIp+":5001"
);
await FirebaseStorage.instance.useEmulator(
host: localIp,
port: 9199,
);

Related

Supabase JS reponse no data property

I tried to configure a next js project with supabase
But the data I receive is not consistent
To do this I have configured the following 3 files :
.env.local
NEXT_PUBLIC_SUPABASE_URL= ********
NEXT_PUBLIC_SUPABASE_ANON_KEY= ********
supabase.js
import { createClient } from '#supabase/supabase-js'
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
)
export default supabase;
pages/api/posts.js
import supabase from '../../utils/supabase';
export default function handler(req, res) {
const posts = supabase.from("posts").select("*");
res.status(200).json(posts);
}
When I console log my posts variable, I have no data property.
It returned this,
PostgrestFilterBuilder {
fetch: [Function (anonymous)],
shouldThrowOnError: false,
allowEmpty: false,
url: URL {
href: '********/rest/v1/posts?select=*',
origin: '********',
protocol: 'https:',
username: '',
password: '',
host: '********',
hostname: '********',
port: '',
pathname: '/rest/v1/posts',
search: '?select=*',
searchParams: URLSearchParams { 'select' => '*' },
hash: ''
},
headers: {
'X-Client-Info': 'supabase-js/1.35.4',
apikey: '********',
Authorization: 'Bearer ********'
},
schema: 'public',
_subscription: null,
_realtime: RealtimeClient {
accessToken: null,
channels: [],
endPoint: 'wss://********/realtime/v1/websocket',
headers: { 'X-Client-Info': 'supabase-js/1.35.4' },
params: {
apikey: '********'
},
timeout: 10000,
transport: [Function: W3CWebSocket],
heartbeatIntervalMs: 30000,
longpollerTimeout: 20000,
heartbeatTimer: undefined,
pendingHeartbeatRef: null,
ref: 0,
logger: [Function: noop],
conn: null,
sendBuffer: [],
serializer: Serializer { HEADER_LENGTH: 1 },
stateChangeCallbacks: { open: [], close: [], error: [], message: [] },
reconnectAfterMs: [Function (anonymous)],
encode: [Function (anonymous)],
decode: [Function: bound decode],
reconnectTimer: Timer {
callback: [Function (anonymous)],
timerCalc: [Function (anonymous)],
timer: undefined,
tries: 0
}
},
_headers: {
'X-Client-Info': 'supabase-js/1.35.4',
apikey: '********',
Authorization: 'Bearer ********'
},
_schema: 'public',
_table: 'posts',
method: 'GET',
cs: [Function: contains],
cd: [Function: containedBy],
sl: [Function: rangeLt],
sr: [Function: rangeGt],
nxl: [Function: rangeGte],
nxr: [Function: rangeLte],
adj: [Function: rangeAdjacent],
ov: [Function: overlaps]
}
Also when I look at the dashboard of my supabase database I see that no query is receive
It seems that this query is not sent to the supabase API but I have no idea why ?
You need to await the Promise: https://supabase.com/docs/reference/javascript/select
import supabase from '../../utils/supabase';
export default async function handler(req, res) {
const posts = await supabase.from("posts").select("*");
res.status(200).json(posts);
}

Firebase hosting test hosting configuration settings

I am looking for a method to test the current provided hosting configuration for Firebase Hosting, the firebase.json file seems to deploy correctly, but there doesn't seem to be a way to see this configuration on the cloud once its deployed:
firebase.json
{
"hosting": {
"public": "dist",
"rewrites": [
{
"source": "/api/v1/**",
"function": "webApp"
},
{
"source": "**",
"destination": "/index.html"
}
],
"headers": [
{
"source": "**/*",
"headers": [
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "Content-Security-Policy",
"value": "frame-ancestors 'self' *://*.hellosign.com"
}
]
}
]
},
"emulators": {
"functions": {
"host": "0.0.0.0",
"port": 5001
},
"firestore": {
"host": "0.0.0.0",
"port": 8080
},
"database": {
"host": "0.0.0.0",
"port": 9000
},
"hosting": {
"host": "0.0.0.0",
"port": 5000
},
"pubsub": {
"host": "0.0.0.0",
"port": 8085
},
"auth": {
"host": "0.0.0.0",
"port": 9099
},
"ui": {
"enabled": true,
"host": "0.0.0.0",
"port": 4000
}
},
"firestore": {
"rules": "firestore.rules"
}
}
The documentation states:
You can check the deployed firebase.json content using the Hosting REST API.
You will need to use that API to check that your release matches your expectations. You won't be able to see this in the console. If console access what you're looking for, then file a feature request with Firebase support.

Merge/Combine top level of dictionary

I am trying to create dictionary out of the servers stored in different env variables in ansible.
What i currently have is:
env_loadbalancer_vservers2: "{{ hostvars[inventory_hostname] | dict2items | selectattr('key', 'match', 'env_.*_loadbalancer_vservers(?![_.])') | list | items2dict }} "
Which will:
get all variables in ansible for a specific host,
change dict to items type
as we can easily access now key value I will match only keys I want using regex
Change it back to list
Back to dict
problem is that output looks like this:
{
"env_decision_manager_loadbalancer_vservers": {
"decision_central": {
"ip_or_dns": "ip",
"port": "port",
"protocol": "SSL",
"ssl": true,
"timeout": 600,
}
},
"env_ftp_loadbalancer_vservers": {
"ftp_1": {
"ip_or_dns": "ip",
"port": "port",
"protocol": "FTP",
"ssl": false,
"timeout": 9010,
}
},
"env_jboss_loadbalancer_vservers": {
"jboss": {
"ip_or_dns": "ip",
"port": "port",
"protocol": "SSL",
"ssl": true,
"timeout": 600,
}
"jboss_adm": {
"ip_or_dns": "som_other_ip",
"port": "rando_number",
"protocol": "SSL",
"ssl": true,
"timeout": 86410,
}
}
While my desired output should look like:
{
"decision_central": {
"ip_or_dns": "ip",
"port": "port",
"protocol": "SSL",
"ssl": true,
"timeout": 600,
},
"ftp_1": {
"ip_or_dns": "ip",
"port": "port",
"protocol": "FTP",
"ssl": false,
"timeout": 9010,
},
"jboss": {
"ip_or_dns": "ip",
"port": "port",
"protocol": "SSL",
"ssl": true,
"timeout": 600,
},
"jboss_adm": {
"ip_or_dns": "som_other_ip",
"port": "rando_number",
"protocol": "SSL",
"ssl": true,
"timeout": 86410,
}
So practically I need to remove "Top-level key tier" and merge their values. I've spent quite a time on this solution without any good progress and I would be happy for any advice :)
PS. The solution should be "clean" without any custom modules or actual tasks, the best idea would just add some functions to the filter pipeline mentioned above that will result in the correct format of dict
Thank you :)
Select the attribute value
regexp: 'env_.*_loadbalancer_vservers(?![_.])'
l1: "{{ hostvars[inventory_hostname]|
dict2items|
selectattr('key', 'match', regexp)|
map(attribute='value')|
list }}"
gives the list
l1:
- decision_central:
ip_or_dns: ip
port: port
protocol: SSL
ssl: true
timeout: 600
- ftp_1: null
ip_or_dns: IP
port: port
protocol: FTP
ssl: false
timeout: 9010
- jboss:
ip_or_dns: ip
port: port
protocol: SSL
ssl: true
timeout: 600
jboss_adm:
ip_or_dns: som_other_ip
port: rando_number
protocol: SSL
ssl: true
timeout: 86410
Combine the items of the list
d1: "{{ {}|combine(l1) }}"
gives the dictionary you're looking for
d1:
decision_central:
ip_or_dns: ip
port: port
protocol: SSL
ssl: true
timeout: 600
ftp_1:
ip_or_dns: ip
port: port
protocol: FTP
ssl: false
timeout: 9010
jboss:
ip_or_dns: ip
port: port
protocol: SSL
ssl: true
timeout: 600
jboss_adm:
ip_or_dns: som_other_ip
port: rando_number
protocol: SSL
ssl: true
timeout: 86410

Deploy nextjs app to firebase fails but works fine in emulator

My next.js app works fine when using the firebase emulator with the below configuration but the same doesn't work when deployed to firebase.
Error from firebase functions log
Error: Failed to load /500
at loadComponents (/workspace/node_modules/next/dist/next-server/server/load-components.js:1:1554)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async Server.findPageComponents (/workspace/node_modules/next/dist/next-server/server/next-server.js:76:257)
at async Server.renderErrorToHTML (/workspace/node_modules/next/dist/next-server/server/next-server.js:138:198)
at async Server.renderToHTML (/workspace/node_modules/next/dist/next-server/server/next-server.js:137:1629)
at async Server.render (/workspace/node_modules/next/dist/next-server/server/next-server.js:74:255)
at async Object.fn (/workspace/node_modules/next/dist/next-server/server/next-server.js:58:672)
at async Router.execute (/workspace/node_modules/next/dist/next-server/server/router.js:25:67)
at async Server.run (/workspace/node_modules/next/dist/next-server/server/next-server.js:68:1042)
at async Server.handleRequest (/workspace/node_modules/next/dist/next-server/server/next-server.js:32:504)
Error: Failed to load /
at loadComponents (/workspace/node_modules/next/dist/next-server/server/load-components.js:1:1554)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async Server.findPageComponents (/workspace/node_modules/next/dist/next-server/server/next-server.js:76:257)
at async Server.renderToHTML (/workspace/node_modules/next/dist/next-server/server/next-server.js:137:542)
at async Server.render (/workspace/node_modules/next/dist/next-server/server/next-server.js:74:255)
at async Object.fn (/workspace/node_modules/next/dist/next-server/server/next-server.js:58:672)
firebase.json
{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
},
"hosting": {
"public": "public",
"cleanUrls": true,
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"function": "ssrServer"
}
]
},
"emulators": {
"auth": {
"port": 9099
},
"functions": {
"port": 5001
},
"firestore": {
"port": 8080
},
"ui": {
"enabled": true
},
"hosting": {
"port": 5008,
"host": "0.0.0.0",
"public": "public"
}
}
}
firebase function
import * as functions from 'firebase-functions';
import next from 'next';
const firebaseFunction = functions;
const server = next({
dev: false,
dir: __dirname + '/public',
conf: {
distDir: '.next',
experimental: {
reactRoot: false,
turboMode: false,
},
future: {},
},
});
const nextjsHandle = server.getRequestHandler();
exports.ssrServer = firebaseFunction.https.onRequest(async (req, res) => {
return server.prepare().then(() => nextjsHandle(req, res));
});
What am I missing?
The errors are indicating that the components have not loaded. Before you deploy to firebase, ensure you run each command respectively
npm run dev
npm run build
Are you running Next 10.2? It could be related to this issue.

Firebase Emulator Firestore not connecting to flutter

I have set up my firebase emulator up an running
On my main programm, at the start of my flutter web app I have the following code:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseFirestore.instance.settings = Settings(
host: '127.0.0.1:8080',
sslEnabled: false,
);
Yet when I try to get the document I get the following error:
Failed to get Document because the client is offline
My firebase.json is the following:
{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"hosting": {
"public": "build/web",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
},
"emulators": {
"firestore": {
"port": 8080
},
"hosting": {
"port": 5000
},
"ui": {
"enabled": true
}
}
}
Turns out everything was set up correctly. The reason I could not communicate with the Firebase Emulator was my Karspersky antivirus. If anyone else has the same issue try to disable your additional antivirus.

Resources