Custom events
Custom events let programs, on computers and on devices, tell each other things. They are how autonomous devices cooperate.
Sending
Section titled “Sending”events.emit(event, value)
Advanced: send a custom event to every program on this network that handles on events.custom("name", value). Returns how many programs received it.
events.emit("gate.open", true)events.send(alias, event, value)
Advanced: send a custom event to the program running on one named device.
events.send("siren", "alarm.test", {seconds: 5})Receiving
Section titled “Receiving”| Event | When it runs |
|---|---|
events.custom("name", value, sender) | Runs when another program on the network sends this custom event. sender has name, kind and id. |
The first value in the parentheses is the event name, a filter fixed when the program starts. The handler receives the value and the sender:
on events.custom("security.alert", alert, sender) { print(sender.kind + " " + str(sender.name) + " reports " + alert.who)}sender is {name, kind, id} of the device whose program sent the event, or the computer. The server fills it in, so
it cannot be faked. Check sender.kind or sender.name before trusting a request.
- A program only reaches programs on its own connected network. Different networks never exchange custom events.
events.emitreaches every program that handles the name;events.sendreaches only the program on the named device.- Both return how many programs received the event.
0means nobody listened, which is not an error. - A custom event never triggers a hardware handler, and receiving one never operates hardware by itself. A receiver decides what to do.
- Delivery happens on a later tick. If a receiver’s queue of 32 events is full, the event is dropped for it.
Limits
Section titled “Limits”| Limit | Value | Error |
|---|---|---|
| Name | starts with a letter; 1 to 64 letters, digits or _ . : - |
event.invalid |
| Value | text, numbers, booleans, nil, lists, tuples and dictionaries; at most 8 levels, 512 values, 16,384 characters and 4,096 bytes encoded |
event.invalid, event.limit |
| Sends | 16 per program per tick | event.limit |
| Receivers | 64 per broadcast |
Device handles, constants such as Direction.NORTH and class objects cannot be sent: send names, str(constant)
and plain data instead.
See the structured custom events tutorial.