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.
//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));
}
})
| Myna. Event | Class for managing distributed events |
| Functions | |
| Event | Creates a new Event object for a particular event name |
| fire | fires this event |
| listen | registers a listener for this event |
| getListeners | returns a tree of instance local event listeners |
| removeListeners | removes listeners from this instance, and will not automatically re-register them during server start |
| persistListeners | internal function to save registered listeners |
Myna.Event=function( name )
Creates a new Event object for a particular event name
| name | name of this event |
var event = new Myna.Event("MyEvent");
Myna.Event.prototype.fire = function( data )
fires this event
| data | data to sent to event listeners. This data must be serializable via Object.toJson() Example: |
new Myna.Event("MyEvent").fire($req.data);registers a listener for this event
| options | options object. see below |
| server | Optional, default null If defined, only listen to events from this server |
| instance | Optional, default null If defined, only listen to events from this instance |
| purpose | Optional, default null If defined, only listen to events from this purpose |
| handler | Optional, default null Handler function. If defined, this will be called with the event packet (see below) as its argument |
| path | Optional, default null MynaPath to handler script. If defined, this will be called included with the event packet (see below) as its “this” scope |
| url | Optional, 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 |
| username | Optional, default null if defined, this is used as the HTTP Basic Auth user for the URL post. |
| password | Optional, default null if defined, this is used as the HTTP Basic Auth password for the URL post. |
| isTemp | Optional, 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. |
| name | The name of this event |
| ts | Date object representing the time the event was fired |
| server | host name of the server that fired this event:$server.hostName, |
| instance | The id of then instance that fired this event |
| purpose | The purpose of the instance that fired this event |
| data | The data supplied to this event |
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"
})Creates a new Event object for a particular event name
Myna.Event=function( name )
fires this event
Myna.Event.prototype.fire = function( data )
returns a tree of instance local event listeners
Myna.Event.getListeners = function()