title | description |
---|---|
Shared Mouse Tracker |
A real-time interactive application for tracking mouse movements across multiple users |
A real-time interactive application built using Next.js and Geobase that tracks mouse movements of multiple users and displays them on a shared canvas. The app features responsive design, real-time updates, and user-specific markers for enhanced clarity.
- Real-Time Mouse Tracking: Track and visualize user mouse movements on a shared canvas
- Unique User Identification: Session-specific user IDs generated using uuid
- Live Updates: Real-time data synchronization using Geobase
- Responsive Design: Mobile-friendly UI built with Tailwind CSS
- Interactive Canvas: Color-coded markers for different users (blue: current user, red: others)
- React
- Next.js
- Geobase
- Tailwind CSS
- uuid
- Node.js (v14 or higher)
- npm or yarn
- Geobase account
- Clone the repository:
git clone https://github.com/decision-labs/geobase-blueprint-realtime.git
cd geobase-blueprint-realtime
- Install dependencies:
npm install
- Configure environment variables:
Create a .env.local
file in the project root:
NEXT_PUBLIC_Geobase_URL=<your-Geobase-url>
NEXT_PUBLIC_Geobase_ANON_KEY=<your-Geobase-anon-key>
- Start the development server:
npm run dev
- Open http://localhost:3000 in your browser.
The main component responsible for displaying and tracking mouse movements:
useEffect(() => {
const canvas = canvasRef.current;
if (!canvas) return;
const ctx = canvas.getContext('2d');
if (!ctx) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
mouseMovements.forEach(({ x, y, user_id }) => {
ctx.fillStyle = user_id === userId ? 'blue' : 'red';
ctx.beginPath();
ctx.arc(x, y, 5, 0, Math.PI * 2);
ctx.fill();
});
setStatus('Idle');
}, [mouseMovements, userId]);
Implementation of Geobase's real-time subscription for mouse movements:
useEffect(() => {
const subscription = Geobase
.channel('public:mouse_movements')
.on(
'postgres_changes',
{ event: 'INSERT', schema: 'public', table: 'mouse_movements' },
(payload: { new: MouseMovement }) => {
const newMovement = payload.new;
setMouseMovements((prev) => [...prev, newMovement]);
}
)
.subscribe();
return () => {
Geobase.removeChannel(subscription);
};
}, []);
Create the required table in your Geobase instance:
CREATE TABLE mouse_movements (
id SERIAL PRIMARY KEY,
user_id UUID NOT NULL,
x INT NOT NULL,
y INT NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
Displays user information and tracking status:
- Shows current user ID
- Indicates real-time tracking status (Idle/Tracking)
Provides canvas management functionality:
- Clear canvas option
- Styled with Tailwind CSS for consistent design
Main container component:
- Wraps the UI elements
- Ensures consistent spacing and layout
- Responsive design for all screen sizes
The application uses Tailwind CSS for a mobile-first approach:
- Flexible Canvas: Automatically scales to fit different screen sizes
- Touch Support: Works on mobile devices and tablets
- Responsive Layout: Adapts UI elements for optimal viewing on all devices
- Interactive Elements: Proper spacing for touch targets
- Visual Feedback: Hover and active states for better UX