Closed duckbytes closed 2 years ago
Hi @duckbytes 👋 thanks for raising this issue. Unfortunately, I was not able to reproduce this behavior on the latest version of aws-amplify
on Chrome or Edge. Both worked as well as Firefox. Are you still experiencing this behavior?
code
const [todos, setTodos] = useState([]);
async function fetchTodos() {
try {
const todos = await DataStore.query(Todo);
setTodos(todos);
} catch (error) {
console.log(error);
}
}
useEffect(() => {
const dataStoreListener = async (capsule) => {
const { channel, payload, source } = capsule;
console.log({ channel, payload, source });
};
Hub.listen("datastore", dataStoreListener);
fetchTodos();
return () => Hub.remove("datastore", dataStoreListener);
}, []);
Edge
Chrome
Firefox
Hi @chrisbonifacio
I put together a minimal reproduction in React:
import "./App.css";
import Amplify, { DataStore, Hub } from "aws-amplify";
import { withAuthenticator } from "@aws-amplify/ui-react";
import { useEffect } from "react";
import { useState } from "react";
import awsconfig from "./aws-exports";
import * as models from "./models";
Amplify.configure(awsconfig);
function App() {
const [message, setMessage] = useState("unknown");
useEffect(() => {
DataStore.start();
Hub.listen("datastore", (hubData) => {
const { event, data } = hubData.payload;
console.log("datastore event", data, event);
if (event === "networkStatus") {
const result = data.active ? "online" : "offline";
setMessage(`User is ${result}`);
}
});
}, []);
return <div className="App">{message}</div>;
}
export default withAuthenticator(App);
and published it for more testing. It looks like the problem is platform specific.
With Chrome on macOS and Windows the message changes to "User is offline" as expected when the network interface is disabled. On Linux and Android the message initially says "User is online" but does not change to offline when the interface is disabled or airplane mode is enabled. An initial online event is sent but not offline events. I couldn't test on iOS.
It would be helpful for this to work at least on Android devices as I'd like to distribute it as a PWA and let the user know when they have lost their connection.
This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server amplify-help
forum.
Before opening, please confirm:
JavaScript Framework
React
Amplify APIs
DataStore
Amplify Categories
api
Environment information
Describe the bug
When listening for datastore events with Hub.listen, the networkStatus event is never emitted even if the user goes offline or online. This problem happens using Chrome or Edge, but in Firefox it works fine.
I am using redux-saga and a channel to monitor network status.
Expected behavior
When the user goes offline I expect a networkStatus event to be emitted from Hub.listen.
Reproduction steps
Code Snippet
Log output
aws-exports.js
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response