A simple but effective realtime location tracking application built with Node.js, Express, Socket.io, and Leaflet.js. This project demonstrates how to track a user's location in a web browser and display it on a map for all connected clients to see in realtime.
- Live Location Tracking: Uses the browser's Geolocation API for accurate, realtime location data.
- IP Geolocation Fallback: If the Geolocation API is unavailable or denied, it gracefully falls back to an IP-based location service to provide an approximate location.
- Realtime Map Updates: Markers on a Leaflet.js map move smoothly as location data is received.
- Multi-User Support: Shows markers for all connected users, each with a unique ID.
- Clean & Responsive UI: A simple interface that works on both desktop and mobile browsers.
- WebSocket Communication: Efficient, bidirectional communication between the client and server using Socket.io.
- Backend:
- Node.js: JavaScript runtime environment.
- Express.js: Web framework for Node.js.
- Socket.io: Library for realtime, bidirectional and event-based communication.
- Frontend:
- EJS: Embedded JavaScript templating.
- Leaflet.js: An open-source JavaScript library for mobile-friendly interactive maps.
- Vanilla JavaScript (ES6+).
Follow these instructions to get a copy of the project up and running on your local machine for development and testing purposes.
Make sure you have the following installed on your system:
-
Clone the repository:
Open your terminal and run the following command to clone the project:
git clone https://github.com/VisXhal06/Realtime_tracker.git
-
Navigate to the project directory:
cd Realtime-tracker-backend -
Install dependencies:
Install all the required npm packages.
npm install
-
Start the server:
You can run the application with the following command:
node app.js
Or, for a better development experience with automatic restarts on file changes, you can install
nodemon:npm install -g nodemon nodemon app.js
-
View the application:
Open your web browser and navigate to: http://localhost:3000
To see the multi-user feature in action, open the link in multiple browser tabs or on different devices on the same network.
The application follows a simple client-server architecture:
- Client Connection: When a user opens the web page, the browser connects to the server using Socket.io.
- Location Request: The client-side JavaScript attempts to get the user's location using the
navigator.geolocationAPI. - Geolocation Fallback: If the Geolocation API fails (e.g., permission denied, timeout), the client makes a request to
https://ip-api.com/jsonto get an approximate location based on the user's IP address. - Emit Location: The obtained coordinates (either from Geolocation or IP lookup) are sent to the server via a
send-locationsocket event. - Broadcast Location: The server receives the coordinates and broadcasts them to all connected clients using a
receive-locationevent. This event includes the user's unique socket ID. - Update Map: Each client listens for the
receive-locationevent. When it receives data, it either creates a new marker on the Leaflet map for a new user or updates the position of an existing marker. - User Disconnect: When a user disconnects, the server emits a
user-disconnectedevent with the user's ID, allowing clients to remove the corresponding marker from the map.
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.
This project is licensed under the ISC License.