---
title: "Listener scripts"
description: "TapHome listener scripts: process incoming MQTT, TCP and UDP messages, read topic and payload from RECEIVEDMSG and bytes from RECEIVEDBYTES."
url: "https://taphome.com/en/docs/script/listener-scripts/"
language: en
lastmod: 2026-10-13
generator: hugo
ai:
  topics: ["packet-parser","listener-script","mqtt","tcp-udp","scripting-language"]
  questions: ["When does a listener script run?","How do I process incoming MQTT messages in a listener script?","What properties does RECEIVEDMSG have?","What is the difference between RECEIVEDMSG and RECEIVEDBYTES?","How do I read bytes of a TCP message with UNPACKVALUE?"]
---

# Listener scripts

TapHome Packet parser listener scripts — process incoming TCP, UDP and MQTT messages with RECEIVEDMSG (topic, payload, protocol) and the older RECEIVEDBYTES byte collection.

Last updated: 2026-10-13

A listener script of a Packet parser module or device processes incoming messages — TCP and UDP packets, including answers to a request sent with `SENDDATA`, and MQTT messages. The message is in the constant `RECEIVEDMSG`, or in the older `RECEIVEDBYTES`. Requests and commands are sent with the [Packet parser functions](https://taphome.com/en/docs/script/packet-parser/).

## Listener script

The listener script runs for every message the module receives — a TCP or UDP packet, or an MQTT message whose topic matches the topic filter of the device or module that owns the listener script — and reads it from [RECEIVEDMSG](https://taphome.com/en/docs/script/listener-scripts/#receivedmsg).

**Remarks**

- An MQTT broker can deliver hundreds of messages a minute. Set the topic filter as narrow as possible, check the topic first and finish with `RETURN(0)` for messages you do not need.
- Empty messages do not start the script.
- More on topic filters in [Listener script](https://taphome.com/en/docs/integration/packet-parser/manual/#listener-script).

**Example — MQTT listener of a Shelly plug**

```taphome
# ignore messages of other devices
IF INDEXOF(RECEIVEDMSG.TOPIC, "shellies/" + plugTopic) <> 0
    RETURN(0);
END

# relay topic: read on or off
IF RECEIVEDMSG.TOPIC = "shellies/" + plugTopic + "/relay/0"
    VAR value := TOSTRING(RECEIVEDMSG.PAYLOAD);
    IF value = "on"
        St := 1;
    ELSEIF value = "off"
        St := 0;
    END
ELSEIF RECEIVEDMSG.TOPIC = "shellies/" + plugTopic + "/temperature"
    # temperature topic: store the value
    internalTemp := TODOUBLE(TOSTRING(RECEIVEDMSG.PAYLOAD));
END
```

## RECEIVEDMSG

`RECEIVEDMSG` holds the message that started the listener script, as a `receivedmessage` value with the payload and, for MQTT, the topic and metadata.

| Property | Type | Meaning |
|---|---|---|
| `PROTOCOL` | string | `"tcp"`, `"udp"` or `"mqtt"`. |
| `PAYLOAD` | blob | Raw payload. Read it as text with `TOSTRING(RECEIVEDMSG.PAYLOAD)`, a single byte with `GETAT`. |
| `LENGTH` | number | Payload length in bytes. |
| `TOPIC` | string | MQTT topic. |
| `CONTENTTYPE`, `CLIENTID` | string | MQTT 5 metadata. |
| `PAYLOADFORMAT` | enum | MQTT 5 payload format indicator. |
| `EXPIRY` | number | MQTT 5 message expiry interval in seconds. |
| `DUP` | bool | MQTT duplicate flag. |

**Remarks** — For TCP and UDP only `PROTOCOL`, `PAYLOAD` and `LENGTH` carry data. The payload is a blob, which `UNPACKVALUE` does not accept — use [RECEIVEDBYTES](https://taphome.com/en/docs/script/listener-scripts/#receivedbytes) for that.

**Available** — Core 2024.1 or newer.

## RECEIVEDBYTES

`RECEIVEDBYTES` is the older form of the received message: a byte collection (`collection<uint8>`) that still works and that `UNPACKVALUE` and `GETAT` can read directly.

**Remarks**

- For TCP and UDP it is the raw payload.
- For MQTT it is a JSON text with `Topic`, `Payload`, `PayloadRaw`, `Dup`, `ContentType`, `PayloadFormatIndicator` and `MessageExpiryInterval`, and payloads over 64 kB stop the script. Prefer `RECEIVEDMSG`, which is faster.

**Example — TCP answer to a request sent with SENDDATA**

```taphome
VAR cmd := GETAT(RECEIVEDBYTES, 0);
IF cmd = 0
    Rc := (GETAT(RECEIVEDBYTES, 1) & 0x01) > 0;
END
```

**See also** — [Binary data](https://taphome.com/en/docs/script/binary-data/), [Parsing functions](https://taphome.com/en/docs/script/parsing-functions/).
