Myna. Event

Class for managing distributed events

Myna instances automatically cluster based on a shared myna_permissions datasource.  Myna clusters have a distributed event system where arbitrary events can be fired by name, and all cluster members may register listeners to handle those events.  All event handling is asynchronous and cannot send any content back to the browser.

Example

//on sender machine
var e = new Myna.Event("user_logged_in)

e.fire($req.data);//this gets converted to JSON


//on listening machine
//This should typically be set in application start or server start
//once a listener is registered, Myna remmebers to re-register it on
//server start
event.listen({
server:$server.hostName,
purpose:$server.purpose,
instance:$server.instance_id,
handler:function(event){
Myna.log("debug","got event " + event.name ,Myna.dump(event));
}
})
Summary
Myna. EventClass for managing distributed events
Functions
EventCreates a new Event object for a particular event name
firefires this event
listenregisters a listener for this event
getListenersreturns a tree of instance local event listeners
removeListenersremoves listeners from this instance, and will not automatically re-register them during server start
persistListenersinternal function to save registered listeners

Functions

Event

Myna.Event=function(name)

Creates a new Event object for a particular event name

Parameters

namename of this event

Example

var event = new Myna.Event("MyEvent");

fire

Myna.Event.prototype.fire = function(data)

fires this event

Parameters

datadata to sent to event listeners.  This data must be serializable via Object.toJson() Example:
new Myna.Event("MyEvent").fire($req.data);

listen

registers a listener for this event

Parameters

optionsoptions object. see below

Options

serverOptional, default null If defined, only listen to events from this server
instanceOptional, default null If defined, only listen to events from this instance
purposeOptional, default null If defined, only listen to events from this purpose
handlerOptional, default null Handler function.  If defined, this will be called with the event packet (see below) as its argument
pathOptional, default null MynaPath to handler script.  If defined, this will be called included with the event packet (see below) as its “this” scope
urlOptional, default null URL of handler.  If defined, this will be called via Myna.HttpConnection with the event packet (see below) as its parameters.  Before calling this URL, the event packet’s data property will be converted to JSON
usernameOptional, default null if defined, this is used as the HTTP Basic Auth user for the URL post.
passwordOptional, default null if defined, this is used as the HTTP Basic Auth password for the URL post.
isTempOptional, default false Normally listeners are registered with the server, and reloaded during server startup.  Set this to “true” to disable behavior for one-use events Note: One of handler, path, or url must be defined.

Event Packet

nameThe name of this event
tsDate object representing the time the event was fired
serverhost name of the server that fired this event:$server.hostName,
instanceThe id of then instance that fired this event
purposeThe purpose of the instance that fired this event
dataThe data supplied to this event

Example

var event = new Myna.Event("MyEvent");
//only listen to local events
event.listen({
server:$server.hostName,
purpose:$server.purpose,
instance:$server.instance_id,
handler:function(event){
Myna.log("debug","got event " + event.name ,Myna.dump(event));
}
})

// reuse an existing page as an event listener, and listen for this event
// from any source
event.listen({
path:"/my_app/do_stuff.sjs"
})

//publish event to external source as a form post, with http authentication
event.listen({
url:"https://othersite.com/republish_myna_event.cfm",
username:"myna",
password:"xOAEZBGjsFy+L13KxciKvNipXb8YYxM2"
})

getListeners

Myna.Event.getListeners = function()

returns a tree of instance local event listeners

removeListeners

removes listeners from this instance, and will not automatically re-register them during server start

Parameters

listarray of listener keys

See

persistListeners

internal function to save registered listeners

Myna.Event=function(name)
Creates a new Event object for a particular event name
Myna.Event.prototype.fire = function(data)
fires this event
Myna.Event.getListeners = function()
returns a tree of instance local event listeners
A MynaPath is a file URI String with certain automatic translations.
Connects to HTTP resources