fiery is using an event-based model to allow you to program the
logic. During the life cycle of an app a range of different events will
be triggered and it is possible to add event handlers to these using the
on() method. An event handler is simply a function that
will get called every time an event is fired. Apart from the predefined
life cycle events it is also possible to trigger custom events using the
trigger() method. Manual triggering of life cycle events is
not allowed.
Life cycle Events
Following is a list of all life cycle events. These cannot be triggered manually, but is fired as part of the normal lifetime of a fiery server:
-
start: Will trigger once when the app is started but before it is running. The handlers will receive the app itself as theserverargument as well as any argument passed on from theignite()method. Any return value is discarded. -
resume: Will trigger once after the start event if the app has been started using thereignite()method. The handlers will receive the app itself as theserverargument as well as any argument passed on from thereignite()method. Any return value is discarded. -
end: Will trigger once after the app is stopped. The handlers will receive the app itself as theserverargument. Any return value is discarded. -
cycle-start: Will trigger in the beginning of each loop, before the request queue is flushed. The handlers will receive the app itself as theserverargument. Any return value is discarded. -
cycle-end: Will trigger in the end of each loop, after the request queue is flushed and all delayed, timed, and asynchronous calls have been executed. The handlers will receive the app itself as theserverargument. Any return value is discarded. -
header: Will trigger every time the header of a request is received. The return value of the last called handler is used to determine if further processing of the request will be done. If the return value isTRUEthe request will continue on to normal processing. If the return value isFALSEthe response will be send back and the connection will be closed without retrieving the payload. The handlers will receive the app itself as theserverargument, the client id as theidargument and the request object as therequestargument. -
before-request: Will trigger prior to handling of a request (that is, every time a request is received unless it is short-circuited by theheaderhandlers). The return values of the handlers will be passed on to the request handlers and can thus be used to inject data into the request handlers (e.g. session specific data). The handlers will receive the app itself as theserverargument, the client id as theidargument and the request object as therequestargument. -
request: Will trigger after thebefore-requestevent. This is where the main request handling is done. The return value of the last handler is send back to the client as response. If no handler is registered a404error is returned automatically. If the return value is not a valid response, a500server error is returned instead. The handlers will receive the app itself as theserverargument, the client id as theidargument, the request object as therequestargument, and the list of values created by the before-event handlers as thearg_listargument. -
after-request: Will trigger after therequestevent. This can be used to inspect the response (but not modify it) before it is send to the client. The handlers will receive the app itself as theserverargument, the client id as theidargument, the request object as therequestargument, and the response as theresponseargument. Any return value is discarded. -
before-message: This event is triggered when a websocket message is received. As with thebefore-requestevent the return values of the handlers are passed on to themessagehandlers. Specifically if a'binary'and'message'value is returned they will override the original values in themessageandafter-messagehandler arguments. This can e.g. be used to decode the message once before passing it through themessagehandlers. Thebefore-messagehandlers will receive the app itself as theserverargument, the client id as theidargument, a flag indicating whether the message is binary as thebinaryargument, the message itself as themessageargument, and the request object used to establish the connection with the client as therequestargument. -
message: This event is triggered after thebefore-messageevent and is used for the primary websocket message handling. As with therequestevent, the handlers for themessageevent receives the return values from thebefore-messagehandlers which can be used to e.g. inject session specific data. The message handlers will receive the app itself as theserverargument, the client id as theidargument, a flag indicating whether the message is binary as thebinaryargument, the message itself as themessageargument, the request object used to establish the connection with the client as therequestargument, and the values returned by the before-message handlers as thearg_listargument. Contrary to therequestevent the return values of the handlers are ignored as websocket communication is bidirectional. -
after-message: This event is triggered after themessageevent. It is provided more as an equivalent to theafter-requestevent than out of necessity as there is no final response to inspect and handler can thus just as well be attached to themessageevent. For clear division of server logic, message specific handlers should be attached to themessageevent, whereas general handlers should, if possible, be attached to theafter-messageevent. Theafter-messagehandlers will receive the app itself as theserverargument, the client id as theidargument, a flag indicating whether the message is binary as thebinaryargument, the message itself as themessageargument, and the request object used to establish the connection with the client as therequestargument. -
send: This event is triggered after a websocket message is send to a client. The handlers will receive the app itself as theserverargument, the client id as theidargument and the send message as themessageargument. Any return value is discarded. -
websocket-opened: This event is triggered when a client tries to establish a WebSocket connection to the server. The handler will receive the app itself as theserverargument, the client id as theidargument, and the WebSocket class provided by httpuv as theconnectionargument. Any return value is discarded. Since this event exposes logic from the httpuv package through theconnectionargument it is liable to changes in the httpuv API. -
websocket-closed: This event will be triggered every time a websocket connection is closed. The handlers will receive the app itself as theserverargument, the client id as theidargument and request used to establish the closed connection as therequestargument. Any return value is discarded.
Custom Events
Apart from the predefined events, it is also possible to trigger and listen to custom events. The syntax is as follows:
# Add a handler to the 'new-event' event
id <- app$on('new-event', function() {
message('Event fired')
})
# Trigger the event
app$trigger('new-event')
# Remove the handler
app$off(id)Additional parameters passed on to the trigger() method
will be passed on to the handler. There is no limit to the number of
handlers that can be attached to custom events. When an event is
triggered they will simply be called in the order they have been added.
Triggering a non-existing event is not an error, so plugins are free to
fire off events without worrying about whether handlers have been
added.
Triggering Events Externally:
If a fiery server is running in blocking mode it is not possible to
communicate with it using the trigger() method (though
these can be fired by other callbacks in the server logic). Instead it
is possible to assign a directory to look in for event trigger
instructions. The trigger directory is set using the
trigger_dir field, e.g.:
app$trigger_dir <- '/some/path/to/dir/'Events are triggered by placing an rds file named after
the event in the trigger directory. The file must contain a list, and
the elements of the list will be passed on as arguments to the event
handlers. After the event has been triggered the file will be deleted.
The following command will trigger the external-event on a
server looking in '/some/path/to/dir/':
