# Your first Zenoh app · Zenoh - pub/sub, geo distributed storage, query

Source: https://zenoh.io/docs/getting-started/first-app

# Your first Zenoh app

Let us take a step-by-step approach in putting together your first Zenoh application in Python.
As the first step, let us see how we get some data from a temperature sensor in our kitchen.
Then we see how we can route this data to store and perform some analytics.
Before cranking some code, let's define some terminology.
Zenoh deals with keys/values where each key is a path and is associated to a value. A key looks like just a Unix file system path, such asmyhome/kitchen/temp. The value can be defined with different encodings (string, JSON, raw bytes buffer...).
Let's get started!

## Pub/sub in Zenoh

First thing first, we need to install thezenoh Python library.

```bash
pip install eclipse-zenoh
```

The examples are updated to use the 1.0 version currently in rc, which is why version must be specified in the installation command. You can find more information about the 1.0 changes in themigration guides.
Then, let's write an application,z_sensor.pythat will produce temperature measurements at each second:

```python
import zenoh, random, time

random.seed()

def read_temp():
    return random.randint(15, 30)

if __name__ == "__main__":
    with zenoh.open(zenoh.Config()) as session:
        key = 'myhome/kitchen/temp'
        pub = session.declare_publisher(key)
        while True:
            t = read_temp()
            buf = f"{t}"
            print(f"Putting Data ('{key}': '{buf}')...")
            pub.put(buf)
            time.sleep(1)
```

Now we need a subscriber,z_subscriber.pythat can receive the measurements:

```python
import zenoh, time

def listener(sample):
    print(f"Received {sample.kind} ('{sample.key_expr}': '{sample.payload.to_string()}')")

if __name__ == "__main__":
    with zenoh.open(zenoh.Config()) as session:
        sub = session.declare_subscriber('myhome/kitchen/temp', listener)
        time.sleep(60)
```

Start the subscriber:

```bash
python3 z_subscriber.py
```

The subscriber waits for an update onmyhome/kitchen/temp.
Now startz_sensor.pyas follows

```bash
python3 z_sensor.py
```

You can see the values produced by the sensor being consumed by the subscriber.

## Store and Query in Zenoh

As the next step, let's see how the value generated by a publisher can be stored in Zenoh.
For this, we useZenoh router(zenohd).
By default, a Zenoh router starts without any storage. In order to store the temperature, we need to configure one.
Create azenoh-myhome.json5configuration file for Zenoh with this content:

```json5
{
  plugins: {
    rest: {                        // activate and configure the REST plugin
      http_port: 8000              // with HTTP server listening on port 8000
    },
    storage_manager: {             // activate and configure the storage_manager plugin
      storages: {
        myhome: {                  // configure a "myhome" storage
          key_expr: "myhome/**",   // which subscribes and replies to query on myhome/**
          volume: {                // and using the "memory" volume (always present by default)
            id: "memory"
          }
        }
      }
    }
  }
}
```

Installand start the Zenoh router with this configuration file:

```bash
zenohd -c zenoh-myhome.json5
```

Now the data generated by our temperature sensor is stored in memory.
We can retrieve the latest temperature value stored in Zenoh:

```python
import zenoh

if __name__ == "__main__":
    with zenoh.open(zenoh.Config()) as session:
      replies = session.get('myhome/kitchen/temp')
      for reply in replies:
          try:
              print("Received ('{}': '{}')"
                  .format(reply.ok.key_expr, reply.ok.payload.to_string()))
          except:
              print("Received (ERROR: '{}')"
                  .format(reply.err.payload.to_string()))
```

## Other examples

You can also have a look at the examples provided with each client API:
- Rust:https://github.com/eclipse-zenoh/zenoh/tree/main/examples
- Python:https://github.com/eclipse-zenoh/zenoh-python/tree/main/examples
- C:https://github.com/eclipse-zenoh/zenoh-c/tree/main/examples