Tuesday, October 19, 2010

Multiple Binds/Triggers in JQuery

Today I experimented with Multiple binds/triggers in Jquery. What I wanted to do what to raise an event, lets say "Add Contact" and have multiple other elements react on that. Well, it wasn't as smooth as I expected since you have to trigger an event on a specified element and only that element or a parent element can bind to it.

Example:
Take this very simple webpage. The scenario is that I wan't to trigger an event when clicking on "Add Contact" and on that event something should happen with both my contact list and my twitter widget.
...
<body>
 <div id="main">
  <button id="add_contact">Add Contact </button>
  <div id="contact_list">Contact List: </div>
  <div id="twitter_widget">Twitter Widget: </div>
 </div>
</body>
...

So if I want to trigger an event on the button you have to trigger it on some element. If I trigger it on contact_list then only that element can bind to it, and if I trigger it twitter_widget then contact_list can't bind to it. So the solution is to bind it to both elements.

We'll have to image that I have a variable with the contact object
$('#add_contact').click(function() {
 $('#contact_list, #twitter_widget').trigger('add_contact', contact );
})

And then add binds to both your elements.
$('#contact_list').bind('add_contact', function(e, contact) {
 $(this).append('* ' + contact.name + '
');
});

$('#twitter_widget').bind('add_contact', function(e, contact) {
 $(this).append('> Tweeted: I added ' + contact.name + ' to my contact list, I think this is the beginning of a beautiful friendship.
');
});

I want to finish this post with a lengthy example where I use this technique and also the reverse. Where I have two buttons that trigger the same event on another element. Great for logging. And also an example where one button click triggers more than one event. It know it looks messy in a this post so just copy-paste it to your favorite editor, save as .html and open in a browser and you will see the magic.

Enjoy!
<html>
 <head>
  <title>Welcome to Jquery Multiple Bind Example!</title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.js"></script>
 </head>
 <body bgcolor="white" text="black">
  <center><h1>Welcome to Jquery Multiple Bind Example!</h1></center>

  <p id="add_contact" style="background:#faa; width:300px;">
   <b>Add Contact</b><br/>
   <input id="name"/><br/>
   <button id="add">Add Contact</button><br/>
   <script>
    $('#add').click(function() {
     $('#contact_list, #twitter_widget').trigger('add_contact', $('#name').val() );
     $('#log').trigger('log', 'Contact Added: ' + $('#name').val() );
    })
   </script>
  </p>

  <p id="connect_div" style="background:#faa; width:300px;">
   <b>Connect To Server</b><br/>
   <button id="connect">Connect</button><br/>
   <script>
    $('#connect').click(function() {
     $('#log').trigger('log', 'Connected to the server.' );
    })
   </script>
  </p>

  <p id="contact_list" style="background:#faa; width:300px;">
   <b>Contact List</b><br/>
   <script>
    $('#contact_list').bind('add_contact', function(e, name) {
     $(this).append('* ' + name + '<br>');
    });
   </script>
  </p>

  <p id="twitter_widget" style="background:#faa; width:1000px;">
   <b>Twitter Widget</b><br/>
   <script>
    $('#twitter_widget').bind('add_contact', function(e, name) {
     $(this).append('> Tweeted: I added ' + name + ' to my contact list, I think this is the beginning of a beautiful friendship.<br>');
    });
   </script>
  </p>


  <p id="log" style="background:#faa; width:1000px;">
   <b>Log</b><br/>
   <script>
    $('#log').bind('log', function(e, name) {
     $(this).append(new Date().getTime() + ':' + name + '<br>');
    });
   </script>
  </p>
 </body>
</html>

Saturday, October 16, 2010

Getting started with Ngix and Strophe.js on Ubuntu

In this post I will get you started on Nginx and Strophe.js
If you missed it, check out my post on Getting started with Ejabberd, it's a precondition to get the stuff in this post going.

I am doing this on Ubuntu 10.04, but I am sure you'll get this running on any Linux flavor.
Lets get started!

Installing Nginx
>sudo apt-get install nginx

Change port on Nginx
I am using port 80 for something else so I will change the port to 8080, this port will be used in all subsequent examples. And you should check the root directory, on some systems it's /var/www, but on mine it was /var/www/nginx-default so that's what I'll use in the rest of this example.
>sudo nano /etc/nginx/sites-available/default
server {
listen 8080 default;
server_name localhost;
...
location / {
root /var/www/nginx-default;
index index.html index.htm;
}


Reload Nginx
sudo nginx -s reload
-s is for signal
Note: On some systems, e.g. Kubunut 10.10, nginx does not start by default. In that case you can start it with >sudo nginx

Check that it works
Point your webbrowser to http://localhost:8080/
You now get a message saying Welcome to nginx!
Yay! It works

Download Strophe.js
Download Strophe.js from http://code.stanziq.com/strophe/
Unzip Strophe somewhere
>unzip strophejs-1.0.1.zip -d ~
Create a Symlink from the root web directory to the folder
>sudo ln -s ~/strophejs-1.0.1

Check that you can reach the page
Go to http://localhost:8080/strophejs-1.0.1/examples/echobot.html
You should now see a page with a simple page with a two field form.

Lets try it out!
Enter form
JID: jonas@localhost
Password: jonas
And click "connect"
Strophe is connecting.
Strophe is disconnecting.

It's not working but we don't expect it to. If you investigate with Firebug you will see that the server is trying to connect with BOSH to http://localhost:8080/xmpp-httpbind and since don't have anything there in our Nginx Web Server the connection fail. What we need to do now is add what is called to proxy_pass to /xmpp-httpbind so it directs the traffic to Ejabbers http-bind page, which we set up in the previous post.

Add xmpp-httpbind redirect Nginx
>sudo nano /etc/nginx/sites-available/default
server {
...
location /xmpp-httpbind {
proxy_pass http://localhost:5280/http-bind;
}
...
}

And reload
>sudo nginx -s reload

Start Echobot
Go to http://localhost:8080/strophejs-1.0.1/examples/echobot.html
Enter same details as above and click "connect".
Now you should get...
Strophe is connecting.
Strophe is connected.
ECHOBOT: Send a message to jonas@localhost/15070348051287336618874524 to talk to me.

Great, almost there.
Use an IM, I use Empathy, to connect to localhost with user jonas2@localhost/jonas2 and then send a message to jonas@localhost. Lets send "Test"
Now in the webpage you should see
ECHOBOT: I got a message from jonas2@localhost/c69ef25a: Test
ECHOBOT: I sent jonas2@localhost/c69ef25a: Test

And in Empathy see
sent jonas2@localhost 19:33: Test
received jonas@localhost 19:33: Test


And now you can see that the echobot webpage works. I hope you have enjoyed these two posts on getting started with Ejabberd, Nginx and Strophe.js, if you have any questions or comments feel free to comment on this post.

See you soon!

Friday, October 15, 2010

Getting started with Ejabberd on Ubuntu

This is a post on getting started on Ejabber on Linux. I am using Ubuntu 10.04 but it should be the same on most linux flavors. This is part of a html5-xmpp project I am doing with a friend. More on that later.

Lets get started!

Install Ejabberd
>sudo apt-get install ejabberd

Give user admin rights
To give user admin rights you need to edit the config file.
>sudo nano /etc/ejabberd/ejabberd.cfg
In the file you need to add a user who will be admin. To confuse matters, I will call this user "admin" and put him on the host "localhost" which is default.
%% Admin user
{acl, admin, {user, "admin", "localhost"}}.

Ctrl-X to Exit and Save

Register User
Ejabber comes with a admin command called ejabberctl, with that you can do the most wonderful things. Lets register the user "admin" on "localhost" with the password "secret"
>sudo ejabberdctl register admin localhost secret

See all registered user
To see all the users on a host run "ejabberdctl registered_users"
>sudo ejabberdctl registered_users localhost
ejabberdctl should now return a simple
admin

Register bunch of users
Lets register two users, we will need them in the next post
>sudo ejabberdctl register jonas localhost jonas
>sudo ejabberdctl register jonas2 localhost jonas2
>sudo ejabberdctl register jonas3 localhost jonas3


Unregister User
This is how you unregister users if you need to do that.
>sudo ejabberdctl unregister jonas3 localhost

Restart Server
Lets restart the server
>sudo ejabberdctl restart

Webadmin
There is a webadmin where you can administer and monitor your ejabber server
Go to http://localhost:5280/admin/
Log in with admin/secret to see all the wonderful stats

Get ready for XMPP


Now that we have the server up and running with a bunch of users, we need to check if it's equipped with http-bind so that we can use BOSH for XMPP communication.

Edit config file
Make sure your config file has http-bind enabled
>sudo nano /etc/ejabberd/ejabberd.cfg
It should look like this:
{5280, ejabberd_http, [
http_bind,
http_poll,
web_admin
]}


And add the http_bind module
{modules,
[
...
{mod_http_bind, []},
...
]}

Lets restart the server
sudo ejabberdctl restart

Verify it on the webpage
Go to http://localhost:5280/http-bind
You should now see a text which reads something like
ejabberd mod_http_bind v1.2
An implementation of XMPP over BOSH (XEP-0206)
This web page is only informative. To use HTTP-Bind you need a Jabber/XMPP client that supports it.


Great, now everything seems to be working. Now check out the next post to get Strophe.js and Nginx up and running!

Thursday, October 14, 2010

Setting up QTP with Terminal Emulator

This posts shows the settings for connecting QTP to terminal emulators 3270 and AS400. These settings works for us in my current project and hopefully it'll work for you or at least point you in the right direction.

Settings for attachmate
Vendor: Attachment (WRQ)
Emulator: Extra 7.1
Protocol: 3270




Settings for IBM AS400
Vendor: IBM
Emulator: IBM PCom
Protocol: Auto-Detect