---
title: IoT & Tracking
description: Build IoT applications and real-time tracking systems with NoLag.
---

# IoT & Tracking

Build real-time IoT applications and tracking systems with NoLag.

## Overview

NoLag is ideal for IoT applications that require real-time data streaming from devices. Whether you're tracking vehicles, monitoring sensors, or controlling smart devices, NoLag provides the low-latency infrastructure you need.

## Fleet Tracking Example

```typescript [TypeScript]
import { NoLag } from '@nolag/js-sdk'

const client = NoLag('device_access_token')
await client.connect()

const fleet = client.setApp('fleet-tracker').setRoom('vehicles')

// Subscribe to all vehicle updates
fleet.subscribe('location')
fleet.subscribe('telemetry')

// Handle location updates
fleet.on('location', (data) => {
  updateMapMarker(data.vehicleId, {
    lat: data.latitude,
    lng: data.longitude,
    heading: data.heading,
    speed: data.speed
  })
})

// Handle telemetry data
fleet.on('telemetry', (data) => {
  updateVehicleStatus(data.vehicleId, {
    fuel: data.fuelLevel,
    engine: data.engineStatus,
    battery: data.batteryVoltage
  })
})
```
```python [Python]
from nolag import NoLag

client = NoLag('device_access_token')
await client.connect()

fleet = client.set_app('fleet-tracker').set_room('vehicles')

# Subscribe to all vehicle updates
await fleet.subscribe('location')
await fleet.subscribe('telemetry')

# Handle location updates
def on_location(data, meta):
    update_map_marker(data['vehicleId'], {
        'lat': data['latitude'],
        'lng': data['longitude'],
        'heading': data['heading'],
        'speed': data['speed']
    })

# Handle telemetry data
def on_telemetry(data, meta):
    update_vehicle_status(data['vehicleId'], {
        'fuel': data['fuelLevel'],
        'engine': data['engineStatus'],
        'battery': data['batteryVoltage']
    })

fleet.on('location', on_location)
fleet.on('telemetry', on_telemetry)
```
```go [Go]
package main

import nolag "github.com/NoLagApp/nolag-go"

func main() {
    client := nolag.New("device_access_token")
    client.Connect()

    fleet := client.SetApp("fleet-tracker").SetRoom("vehicles")

    // Subscribe to all vehicle updates with handlers
    fleet.Subscribe("location", func(data any, meta nolag.MessageMeta) {
        d, _ := data.(map[string]any)
        updateMapMarker(d["vehicleId"].(string), MapPosition{
            Lat:     d["latitude"].(float64),
            Lng:     d["longitude"].(float64),
            Heading: d["heading"].(float64),
            Speed:   d["speed"].(float64),
        })
    })

    fleet.Subscribe("telemetry", func(data any, meta nolag.MessageMeta) {
        d, _ := data.(map[string]any)
        updateVehicleStatus(d["vehicleId"].(string), VehicleStatus{
            Fuel:    d["fuelLevel"].(float64),
            Engine:  d["engineStatus"].(string),
            Battery: d["batteryVoltage"].(float64),
        })
    })
}
```

## Device Publishing

On the IoT device, publish updates at regular intervals:

```typescript [TypeScript]
// On the IoT device: publish location updates
setInterval(() => {
  const location = gps.getLocation()

  device.emit('location', {
    vehicleId: DEVICE_ID,
    latitude: location.lat,
    longitude: location.lng,
    heading: location.heading,
    speed: location.speed,
    timestamp: Date.now()
  })
}, 5000) // Every 5 seconds
```
```python [Python]
# On the IoT device: publish location updates
async def publish_location():
    while True:
        location = gps.get_location()

        await device.emit('location', {
            'vehicleId': DEVICE_ID,
            'latitude': location.lat,
            'longitude': location.lng,
            'heading': location.heading,
            'speed': location.speed,
            'timestamp': time.time()
        })

        await asyncio.sleep(5)  # Every 5 seconds
```
```go [Go]
// On the IoT device: publish location updates
func publishLocation() {
    ticker := time.NewTicker(5 * time.Second)
    for range ticker.C {
        location := gps.GetLocation()

        device.Emit("location", map[string]any{
            "vehicleId": DEVICE_ID,
            "latitude":  location.Lat,
            "longitude": location.Lng,
            "heading":   location.Heading,
            "speed":     location.Speed,
            "timestamp": time.Now().Unix(),
        })
    }
}
```

## Use Cases

- **Fleet management** - Track vehicles, deliveries, and drivers
- **Asset tracking** - Monitor equipment and inventory location
- **Sensor networks** - Collect data from environmental sensors
- **Smart home** - Control and monitor IoT devices
- **Wearables** - Stream health and fitness data

## Best Practices

- Use device-type actors for IoT devices
- Batch telemetry data to reduce message frequency
- Use QoS 1 for important status updates
- Implement reconnection logic for unreliable networks
- Consider data retention and historical storage needs

## Next Steps

- [Quality of Service](/docs/concepts/qos)
- [Device Authentication](/docs/authentication)
