<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Arun D</title>
    <description>The latest articles on DEV Community by Arun D (@arunnambissan).</description>
    <link>https://dev.to/arunnambissan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F363407%2F9c8cf795-9531-40c0-9c89-2e2dfc8c2139.jpg</url>
      <title>DEV Community: Arun D</title>
      <link>https://dev.to/arunnambissan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arunnambissan"/>
    <language>en</language>
    <item>
      <title>Custom hook to store user online presence using firebase in React Native</title>
      <dc:creator>Arun D</dc:creator>
      <pubDate>Wed, 26 May 2021 17:51:47 +0000</pubDate>
      <link>https://dev.to/arunnambissan/custom-hook-to-store-user-online-presence-using-firebase-in-react-native-1me9</link>
      <guid>https://dev.to/arunnambissan/custom-hook-to-store-user-online-presence-using-firebase-in-react-native-1me9</guid>
      <description>&lt;p&gt;Here I will show how to create a custom react hook which can be used in your react native application to store the logged in user's online presence.&lt;/p&gt;

&lt;p&gt;I am using the following packages in my project to implement our requirement:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://rnfirebase.io/" rel="noopener noreferrer"&gt;react-native-firebase&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/react-native-netinfo/react-native-netinfo" rel="noopener noreferrer"&gt;react-native-netinfo&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I believe that you have already set up above packages in your project, if not you can refer the above links.&lt;/p&gt;

&lt;h2&gt;
  
  
  Identifying if the user is online
&lt;/h2&gt;

&lt;p&gt;Firstly, we can create a new file for our hook. I am naming it as &lt;code&gt;useOnlinePresence.js&lt;/code&gt; and adding the first few lines of code. Also we can import the required packages to the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import NetInfo from "@react-native-community/netinfo";
import database from '@react-native-firebase/database';
import auth from '@react-native-firebase/auth';

export default useOnlinePresence = () =&amp;gt; {

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can add react's useEffect hook and we can add an event listener inside it, which watches the network state using the package NetInfo.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default useOnlinePresence = () =&amp;gt; {
    useEffect(() =&amp;gt; {
        const unsubscribe = NetInfo.addEventListener(state =&amp;gt; {
            if (state.isConnected) {
                console.log("user online")
            }
        });

        return unsubscribe;
    }, []);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now we can identify when the user gets online and we can save it to firebase realtime database. &lt;/p&gt;

&lt;h2&gt;
  
  
  Saving online presence to database
&lt;/h2&gt;

&lt;p&gt;We can create a new function &lt;code&gt;saveOnlineStatus&lt;/code&gt;, here we can get the authenticated user's uid by calling the firebase auth method &lt;code&gt;auth().currentUser.uid&lt;/code&gt;. Then create a new realtime db reference using the uid and set the value as &lt;code&gt;true&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default useOnlinePresence = () =&amp;gt; {
    const saveOnlineStatus = () =&amp;gt; {
        const userId = auth().currentUser.uid;

        const reference = database().ref(`/online/${userId}`);

        reference.set(true)
        .then(() =&amp;gt; {
             console.log('Online status set as true')
        });
    }

    useEffect(() =&amp;gt; {
        const unsubscribe = NetInfo.addEventListener(state =&amp;gt; {
            if (state.isConnected) {
                saveOnlineStatus();
            }
        });

        return unsubscribe;
    }, []);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deleting online presence from DB
&lt;/h2&gt;

&lt;p&gt;Now we need to delete the saved data once the user goes offline, you can use the below lines of code to achieve that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reference
.onDisconnect()
.remove()
.then(() =&amp;gt; console.log('On disconnect configured'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The firebase server runs the remove method once the user goes offline or the user suddenly quits the application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;So we are done with the custom hook implementation and the &lt;code&gt;useOnlinePresence.js&lt;/code&gt; file is given below. You can call the hook &lt;code&gt;useOnlinePresence&lt;/code&gt; from the layout component which gets rendered after successful login.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect } from "react";
import NetInfo from "@react-native-community/netinfo";
import database from '@react-native-firebase/database';
import auth from '@react-native-firebase/auth'; 

export default useOnlinePresence = () =&amp;gt; {
    const saveOnlineStatus = () =&amp;gt; {
        const userId = auth().currentUser.uid;

        const reference = database().ref(`/online/${userId}`);

        reference.set(true)
        .then(() =&amp;gt; {
             console.log('Online status set as true')
        });

        reference
        .onDisconnect()
        .remove()
        .then(() =&amp;gt; console.log('On disconnect configured'));
    }

    useEffect(() =&amp;gt; {
        const unsubscribe = NetInfo.addEventListener(state =&amp;gt; {
            if (state.isConnected) {
                saveOnlineStatus();
            }
        });

        return unsubscribe;
    }, []);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see the data saved in realtime database of your firebase project - &lt;code&gt;https://console.firebase.google.com/project/&amp;lt;your-project-id&amp;gt;/database&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmswas532pnos540gc0p.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmswas532pnos540gc0p.PNG" alt="Alt Text" width="350" height="61"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;Cover Photo by &lt;a href="https://unsplash.com/@robin_rednine?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;ROBIN WORRALL&lt;/a&gt; on &lt;a href="https://unsplash.com/?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>reactnative</category>
      <category>firebase</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>My Portfolio which mimics an Ubuntu Terminal</title>
      <dc:creator>Arun D</dc:creator>
      <pubDate>Sun, 16 May 2021 17:50:17 +0000</pubDate>
      <link>https://dev.to/arunnambissan/my-portfolio-5h4</link>
      <guid>https://dev.to/arunnambissan/my-portfolio-5h4</guid>
      <description>&lt;p&gt;Check out my Online Portfolio which mimics an Ubuntu Terminal - &lt;a href="https://ArunD.in" rel="noopener noreferrer"&gt;https://ArunD.in&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;I have used React JS, Firebase, Firestore, and Material UI to develop this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1inwho4jhizjj5rho4ko.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1inwho4jhizjj5rho4ko.PNG" alt="Alt Text" width="800" height="435"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>firebase</category>
      <category>terminal</category>
    </item>
  </channel>
</rss>
