views:

110

answers:

2

I'm taking about event function in Nitrogen, the Erlang web framework, in a web module that is run when you get a postback.

Does event always run in the same process for the same web client (that is, browser window or frame).

I've recall at least one case in which that didn't happen, but I can't reproduce it.

+3  A: 

No, Nitrogen events get run in whichever process is currently handling the web request for nitrogen.

Since Nitrogen is a very asynchronous framework, you may have multiple processes handling multiple events for the same browser window or frame asynchronously. This means that you could very well have different events get handled by seperate processes.

Jeremy Wall
So, a button may deliver the same event twice and the events be executed in different processes. Then the answer is No. I'm adding a "No" at the beginning of it just for clarity, feel free to remove it if this comment shows I misinterpret you.
J. Pablo Fernández
The answer is "No" in the sense that Nitrogen does nothing to ensure events will be run in the same process. However if you find an http server that provides such a feature (and is compatible with Nitrogen), it is achievable.
Zed
A: 

Hey guys, i have discovered the process registry for nitrogen called: nprocreg.
When you check your apps folder ( $NITROGEN_INSTALL_DIR/apps/nprocreg ).

In this application, processes can be spawn across more than one nitrogen app servers even across multiple machines. To experiment with this, start 2 erlang nodes on two differnt hosts. ping them so that they are connected. (net_adm:ping(?OTHER_NODE)). Now, start nitrogen on both erlang nodes. You could actually first start the nitrogen and then ping the two nodes
Make sure that the two nitrogen nodes are using long names i.e [NAME]@IP_ADDRESS on a LAN.

Now, on a third machine on your LAN, create a DNS (Name Server) . Put the same name mapping onto two different IP Adresses of our two nodes running the nitrogen applications. Configure the two computers runnind nitrogen to ensure that they point to the DNS IP address for DNS Services (Actually do this for all the computers in your LAN).
You will discover that when you request for a page (residing in the doc root of both nitrogen apps) from several computers on your LAN, using the mapped name in your browser you see that the DNS server does some kind of Load balancing.
Now, Ensure that the page you are requesting for can show you from which nitrogen server its coming from on the interface. This page must have say a button which generates an event that will wf:flash(wf:f("Some statement on the interface",[]))
Now, request this page on two different computers and note where each is being served from. Next, go to one nitrogen server and stop it.
When you now click on the button on the browser which got its page the nitrogen server that we have just put down, it still works. This is because, the event is forwarded to the other nitrogen app by JQuery in the users browser. Remember that the browser caches its DNS response which contains two IP Addresses on the same name. It notices that one of the servers mapped on the name is down and so forwards the event to the next IP address.
This is why, Rusty and the friends discovered that if nitrogen processes can be registered across more nitrogen apps, when run behind a load balancer, the events can be fowarded to any nitrogen app in the cluster.
Of course this works well if you ensure that the two apps on different machines have the same pages, modules and configuration of paths. This is because the event call back function maybe calling say a database API.
An example of load balancing using DNS Server can be seen when you 'dig' google from your terminal on linux or solaris like this:

dig www.google.com
You will see that the name server has the same name mapped onto several Ip adresses. This ensures availability across domains and ensures some kind of load balancing
The DNS server is actually a very important node in web site structuring and with nitrogen framework, the nprocreg server ensures that if several nitrogen servers are run in a cluster, events may become distributed. It takes care of process registry across your cluster so that an event can be excecuted on a different nitrogen server other than the one that served the page in the first place. Play with it and your local DNS Server, you'll see the results /[email protected]

Muzaaya Joshua