Writing a dumpling eater¶
A dumpling eater has a very simple mission: receive dumplings from nd-hub
and do whatever it wants with them. The netmomo and packscape
Web applications are examples of dumpling eaters.
To create an eater with Python you subclass DumplingEater
and
implement on_dumpling()
. All eaters will receive all dumplings being sent
from nd-hub
.
Note
The dumpling hub will emit its own special dumplings describing the hub’s
current status. These are emitted at regular intervals with the
"SystemStatusChef"
chef name. These system status dumplings, like all
other dumplings, are sent to every eater.
The DumplingEater handlers, like on_dumpling()
, are async methods so you
need to define them with the async
keyword.
The following eater prints the payload of every dumpling sent from
nd-hub
:
import json
import netdumplings
class PrinterEater(netdumplings.DumplingEater):
async def on_connect(self, hub_uri, websocket):
print(f'Connected to nd-hub at {hub_uri}')
print('Waiting for dumplings...\n')
async def on_dumpling(self, dumpling):
# The given dumpling is a netdumplings.Dumpling instance.
dumpling_printable = json.dumps(dumpling.payload, indent=4)
print(f'{dumpling_printable}\n')
def dumpling_printer():
eater = PrinterEater()
eater.run()
if __name__ == '__main__':
dumpling_printer()
Writing eaters in languages other than Python¶
Since dumplings are just JSON data sent over a WebSocket connection to and from
nd-hub
, you can write your dumpling eaters in any language you like. If
you do this (or otherwise aren’t using the provided DumplingEater
class) then there’s a few things to remember:
Your eater needs to announce itself when it connects to
nd-hub
by passing a simple payload of{"eater_name": "your_eater_name"}
.Your eater will then receive every dumpling coming out of
nd-hub
. It may want to interrogate themetadata
key of each dumpling to check thechef_name
(or any other information it cares about) to decide whether it’s interested in the dumpling or not. Ignoring unwanted dumplings early is a good idea.