Redom
Redom is a distributed object based server-centric user-friendly web application framework. Redom enables developers to write all application logics in Ruby at server side easily using both browser-side and server-side libraries. Redom provides distributed objects published by browser in natural Ruby syntax so that developers can access browser-side objects directly.
Redom requires Ruby 1.9 or higher.
Getting started
Installation
$ gem install redom
Usage
-
Create a Redom connection class
A Redom connection class is where you write the scripts to manipulate the browser-side objects from server. The Redom connection class must include module Redom::Connection. An instance of Redom connection class will be created when the connection between browser and Redom server is established. There are three methods in Redom::Connection that can be overridden to tell Redom what to do.
- on_open - Called when the connection is established.
- on_close - Called when the connection is closed.
-
on_error(err) - Called when an error occurs.
-
Access to browser-side objects
Every browser-side object is published as a ditributed object. Therefore, method invocation and property reference of these objects can be done as if they are Ruby objects.
Example: document.getElementById('text').value -
Start Redom server
Assuming you have created a Redom connection class and saved it into a fileapp.rb
, you can start Redom server using commandredom app.rb
.
Redom uses WebSocket as the server/browser communication protocol. EM-WebSocket is used as the default WebSocket server in Redom. -
Connect to Redom server from a web page
Once you have added Redom JavaScript runtime into the web page, you can useRedom("ws://localhost:8080").open("RedomConnectionClassName")
to connect to a Redom server. The operations written in the specified Redom connection class will be processed.
Example: Hello World!
hello.rb
require 'redom'
class HelloConnection
inlcude Redom::Connection
def on_open
alert "Hello World!"
end
def on_close
puts "Browser is closed."
end
end
hello.html
<html>
<head>
<title>Hello World!</title>
<script type="text/javascript" src="redom.js"></script>
<script type="text/javascript">
window.onload = function() {
Redom("ws://localhost:8080").open("HelloConnection");
}
</script>
</head>
</html>
See more examples in example
API Docs
(Module) Redom
-
Redom.start(opts = {})
Start Redom server.
Parameter: opts (Hash) - Options. Default values are as below: :log => STDOUT - Log file :log_level => 'error' - Log level. [fatal|error|warn|info|debug] :worker => 5 - Number of worker threads. :buff_size => 200 - Size of bluk messages before synchronization with browser.
-
Redom.stop
Stop Redom server.
(Module) Redom::Connection
-
connections
Return all Redom connection instances in an array that the Redom server is holding currently.
Return:
(Array) - An array of Redom::Connection instancesExample:
connections.each { |conn| conn.do_something }
-
sync{}
Synchronize with browser immediately. Notice that a null block is required.
Return:
nilExample:
a = document.getElementById("a").value b = document.getElementById("b").value sync{} puts a + b
-
sync
Return a synchronous method caller of this connection. Any method of this connection can be called through this method caller. Current process will be blocked until the method invocation is done.
Return:
(Redom::Connection::Sender) - A synchronous method callerExample:
def foo sleep 5 "bar" end p sync.foo # will print "bar" after 5 seconds
-
async { ... }
Evaluate the code inside the block asynchronously which means current process will not be blocked.
Return:
nilExample:
def foo sleep 5 "bar" end p async { p foo } # print "nil" immediately and will print "bar" after 5 seconds
-
async
Return a asynchronous method caller of this connection. Any method of this connection can be called through this method caller and current process will not be blocked. The return value of the method invocation can not be retrieved.
Return:
(Redom::Connection::Sender) - A asynchronous method callerExample:
def foo sleep 5 "bar" end p async.foo # print "nil" immediately and will print "bar" after 5 seconds
-
window, document
A reference for browser-side object 'window' and 'document'.
Returns:
(Redom::Proxy) - A Redom::Proxy that references to browser-side object 'window' and 'document'.
Examples:
window.alert "Hello world."
alert "Hello World." (window can be omitted)
-
parse(src)
Parse Ruby code into JavaScript code.
Parameter:
src (String) - Ruby codeReturn:
(String) - JavaScript code.Example:
window.eval parse("alert 'Hello World.'")
(Class) Redom:Proxy
-
sync
Synchronize with browser immediately and return the true value of the referenced object.
Returns:
(Object) - Primitive type if the referenced object is primitive, otherwise the Redom::Proxy itself.Examples:
value = document.getElementById("text").value.sync
Hints
* Define a event handler as 'object.event_name = :event_handler_name'
Examples:
def button_click_handler(event)
button = event.srcElement
end
document.getElementById("button").onclick = :button_click_handler
License
The [MIT] License - Copyright © 2012 Yi Hu [MIT]: http://www.opensource.org/licenses/mit-license.php
Contact
Email: future.azure@gmail.com