usePresence
newReal-time online/offline presence using Realtime Database.
Usage
usePresence writes the user's online state to a Realtime Database path and automatically removes it on disconnect using Firebase's onDisconnect mechanism.
usePresence
import { usePresence } from 'refirebase/react';
function OnlineIndicator({ userId }) {
const { isOnline } = usePresence('presence/' + userId);
return (
<span
className={isOnline ? 'text-green-500' : 'text-gray-400'}
>
{isOnline ? 'Online' : 'Offline'}
</span>
);
}Custom presence value
Pass a second argument to store any value instead of a simple boolean.
custom value
// Store a rich presence object
const { isOnline } = usePresence('rooms/abc/members/' + userId, {
uid: userId,
joinedAt: new Date().toISOString(),
});Parameters
| Param | Type | Description |
|---|---|---|
path* | string | Realtime Database path to store the presence value. |
userValue | unknown | Value to write when online.(default: true) |
Return value
| Param | Type | Description |
|---|---|---|
isOnline | boolean | True when the presence value exists in the database. |