0 Key, intent and scope
Kristóf Tóth edited this page 2019-12-19 13:47:09 +01:00

TFW JSON messages have some essential fields that are described below.

It is important to understand that the key field is mandatory for all TFW messages and you will be using it all the time, but the intent and scope fields are more of implementation details, only used in rare edge cases internally in the framework and are not intended for general usage by challenge developers.

Key (mandatory)

This field specifies the topic of a message and is used for addressing. Parties interested in certain topics can subscribe to a set of keys to receive messages containing them. It has no default value and it is mandatory (every valid TFW message will have a key). It usually indicates the domain of the message along with a command like message.send. You can define your own keys and send messages with them to the TFW server, but nothing will happen until you create event handlers listening to those keys. Event handlers use prefix matching on the value of the key field to determine if they should receive a given message (i.e. an event handler subscribed to ide will receive both ide.write and ide.read messages).

Intent (optional)

This field has two valid values: "control" and "event". The former is used to instruct an event handler to perform an action and the latter emits information about something (RPC and event advertisement). This distinction is really important, because the lack of intent could lead to infinite recursion in some event handlers. Generally there is no need to explicitly set this field unless you are creating complex event handlers with several available commands and emitted events (most of the time you don't really have to care this exists). The default value is "control" (messages without an intent key are treated as control messages).

Scope (optional)

The TFW server uses this field for routing and it specifies the direction of messages. It has three valid values: "zmq", "websocket", "broadcast". Their names are quite descriptive, the first one forwards the message to the event handlers, the second one addresses the frontend, and the third one broadcasts the message to both directions. Note that messages without a scope field are treated as zmq, therefore there is usually no need to specify this explicitly.