The Fire generator creates a new Fire-object, which is the class containing all the app logic. The class is based on the R6 OO-system and is thus reference-based with methods and data attached to each object, in contrast to the more well known S3 and S4 systems. A fiery server is event driven, which means that it is build up and manipulated by adding event handlers and triggering events. To learn more about the fiery event model, read the event vignette. fiery servers can be modified directly or by attaching plugins. As with events, plugins has its own vignette.
Initialization
A new 'Fire'-object is initialized using the new() method on the generator:
app <- Fire$new(host = '127.0.0.1', port = 8080L)
Copying
As Fire objects are using reference semantics new copies of an app cannot be made simply be assigning it to a new variable. If a true copy of a Fire
object is desired, use the clone() method.
Examples
# Create a New Appapp <- Fire$new(port =4689)# Setup the data every time it startsapp$on('start',function(server,...){ server$set_data('visits',0) server$set_data('cycles',0)})# Count the number of cyclesapp$on('cycle-start',function(server,...){ server$set_data('cycles', server$get_data('cycles')+1)})# Count the number of requestsapp$on('before-request',function(server,...){ server$set_data('visits', server$get_data('visits')+1)})# Handle requestsapp$on('request',function(server,...){ list( status =200L, headers = list('Content-Type'='text/html'), body = paste('This is indeed a test. You are number', server$get_data('visits')))})# Show number of requests in the consoleapp$on('after-request',function(server,...){ message(server$get_data('visits')) flush.console()})# Terminate the server after 300 cyclesapp$on('cycle-end',function(server,...){if(server$get_data('cycles')>300){ message('Ending...') flush.console() server$extinguish()}})# Be politeapp$on('end',function(server){ message('Goodbye') flush.console()})## Not run:app$ignite(showcase =TRUE)## End(Not run)
Active bindings
host: A string giving a valid IPv4 address owned by the server, or '0.0.0.0' to listen on all addresses. The default is '127.0.0.1'
port: An integer giving the port number the server should listen on (defaults to 8080L)
refresh_rate: The interval in seconds between run cycles when running a blocking server (defaults to 0.001)
refresh_rate_nb: The interval in seconds between run cycles when running a non-blocking server (defaults to 1)
trigger_dir: A valid folder where trigger files can be put when running a blocking server (defaults to NULL). See the c("list("The event cycle in fiery")", " vignette") for more information.
plugins: A named list of the already attached plugins. Read Only - can only be modified using the attach() method.
root: The location of the app. Setting this will remove the root value from requests (or decline them with 400 if the request does not match the root). E.g. the path of a request will be changed from /demo/test to /test if root == '/demo'
access_log_format: A glue string defining how requests will be logged. For standard formats see common_log_format and combined_log_format . Defaults to the Common Log Format
event: The name of the event that should trigger the handler
handler: The handler function that should be triggered
pos: The position in the handler stack to place it at. NULL will place it at the end.
Returns
A unique string identifying the handler
Method off()
Remove an event handler from the app.
Usage
Fire$off(handlerId)
Arguments
handlerId: The unique id identifying the handler
Method trigger()
Trigger an event in the app. This will cause any handler attached to the event to be called. See the c("list("The event cycle in fiery")", " vignette") for more information.
Usage
Fire$trigger(event, ...)
Arguments
event: The name of the event
...: Arguments passed on to the handlers
Returns
A named list containing the return values of all handlers attached to the event
Method send()
Send a Websocket message to a client. Will trigger the send event.
Usage
Fire$send(message, id)
Arguments
message: The message to send
id: The id of the client to send to. If missing, the message will be send to all clients
Method close_ws_con()
Close a Websocket connection. Will trigger the websocket-closed event
Usage
Fire$close_ws_con(id)
Arguments
id: The id of the client to close the websocket connection to