Still working the various sources of documents fro...
# off-topic
g
Still working the various sources of documents from repos to website. I need to know if the server is connected as we do offline stuff. Looks like in JS client using a listener with .subscribe((status)=>{if (status==="SUBSCRIBED") setOnline(true); else...... works. Any reason to avoid this as it is not in web docs, but only found in repo?
s
As far as I've tested, the
.subscribe
function will return an error if the connection drops / the device goes offline. Depending on your JS environment, it would probably be best to also implement a network check. For example, with React Native, you could use NetInfo (https://github.com/react-native-netinfo/react-native-netinfo , or for Expo: https://docs.expo.dev/versions/latest/sdk/netinfo/), and then use that as an additional check to confirm if the local network is online.
g
subscribe seems to return a well handled error message versus 'SUBSCRIBED' in the response if I kill the browser online connection. Database reads/writes require a try/catch as they barf. This is a ReactJS/Firebase app trying to move over. My main concern is that these error conditions don't seem well documented (main website docs don't show subscribe returning anything) but the repo code shows 3 forced responses. Just hate to assume something is supported critical to app when not in the API docs.
s
I know for a fact that this works as I use it in a production server-side app:
Copy code
.subscribe(result => {
  if (result !== "SUBSCRIBED") {
    // connection to subscription server dropped
  }
})
In my case, I just log a message out in case I need to debug further, but you could use a react hook to trigger a state update to show your apps 'offline' UI elements. Be aware that at the moment, the main limitation to subscriptions is that they don't support RLS policies - meaning that every message sent via subscription will be sent to everyone that's connected. This is mainly a limitation of Postgres publications not supporting them, so a custom solution is needed. The team are working on a solution for this, but I'm not sure of the current progress of it.
g
Thanks, We do alot more than UI when offline (like caching writes) but I think if I subscribe on the user row by uid and use a "last_note_id" field I get double use in connection and to know if user used another device to add notes. Not sure how RLS comes into play here, but I'll do more digging. Thanks again for the response as I really want us to move from Firebase if possible.
Just to wrap up. I understand now the RLS issue with Realtime it basically makes the table open to anyone if in use.... disappointing, but for now will just use a small table for status update info and then go fetch needed data.