Some features of XMPP

Hello guys,

Welcome back to another exciting blog post on this blog. I hope after reading this post, you will be very excited to work in the field XMPP. Long ago developers used a lot of their important time to write the chatting applications. That was the time when they used the HTTP protocol to write those apps. But the time had changed for good and we found something good as the alternative known as XMPP.

XMPP stands for “Extensible Messaging and Presence Protocol”. As the name suggests, it is the protocol that is used to make the chatting applications. The protocol makes it so easy to create a chatting application. In XMPP all the features are divided into XEP’s. I don’t know the full form of this and I am very lazy to type it in the GOOGLE. But I am sure if you want to you can google it. For example, if you want to implement a chatting application with a feature of Multiple Users in a room( Modern day applications call them groups) then you can find a page on the XMPP website and you can open the page.

xmpp.org/extensions/xep-0045.html.

For every small feature that you can think of is given a name and a number in the XMPP. When you go through these pages you will learn the way in which the chatting applications works.

I can write the whole specs about the XMPP up here. But, I am sure that I will not be able to explain as well as this great developer JACK MOFFITT did in this video.

 

Jack Moffitt also wrote the book on XMPP which is a great read. If you want it you buy it on the amazon. I will sum up some of the basic things discussed by the developer in the video. I have not explained about the concepts like long polling and some more as the video explains it all.

In XMPP the connection is made with the help of JID and a password.

  • JID are divided into two categories, bare JID and full JID.
  • Full JID is like darcy@pemberley.lit/library.
  • Bare JID is like darcy@pemberley.lit
  • Messaging is done with the help of stanzas. Stanzas are the basic sending and receiving method in the case of the XMPP. If you want to know something about a JID on the network send a stanza to the server and server will respond with the answer.
  • Stanzas are divided into three parts on the basic level. These are:
  1. Presence
  2. message
  3. IQ
  • Chatting is a two-way process. It includes the message to be transferred from the client to the server and server sending back the response.  So this set of XML is known as a stream.

If I missed some basic point do write in the comments below.

Now XMPP made our life easier by writing down everything and specifying everything properly. But, that was not enough as the protocols are not enough if you want to build something on the top of the protocol you have to cover a lot of stuff.

For example, let’s say you are writing a chatting application and you start from scratch. Then you have to implement more than 300 small protocols to make the chatting applications. So the great developers like Jack came up with the idea to build something on the top of the protocol and make the procedure easier for the developers who want to implement their own chatting apps. This was the reason that brought the strophe.js library into the picture.

All those developers spent day and night to come up with a simple interface that can talk to the server and the client with the help of mere functions.

I have already tried a lot of chatting applications out there because I did not wanted to start from the scratch. As we all know it a very difficult task in the beginning as you are very new to all the concepts in the code. But as you start grasping the code you come to know what is happening.  Now, this may take time according to how much you already know about the framework and the language.

Now all the clients were built in JavaScript as we wanted to implement the web client. As you guys know that I have been writing the JS code for a long time but still there was something odd about this code. They used namespaces and something called the combination of object-oriented and functional programming.

So this is forcing me to learn the functional programming. So I read a lot of posts and books about this and trust me it went right over my head. All those concepts like Fuctors,  monads etc were the terms that I had never heard. So I tried to skip that part and come back to it in a while. But all those concepts can be said to be interesting.

That is the story for another post. For now, I am trying to implement the Message Archive Management into the JSXC web client. I am in the conversation with the developer of the JSXC who is helping a lot from a few days. Let’s hope that I can add a prototype in the main repo so that we can use this web app.

Here is the github link to the developer of the JSXC who has bee  helping a lot. Do check him out.

https://github.com/sualko

Thanks for reading. Do like the post and Happy coding.

 

Creating graphs using data form .csv file

Yesterday I was talking to a friend and he told me that he has some work that he needs to complete by the following night. He asked me for the help and I decided to help as my exam was postponed. He said that he had a .csv file( comma separated values) and want to shift the whole data into postgresql database.

I told him that I will like to help him and ask him to send me the files. First task was to install postgresql.

$ sudo apt-get install postgresql postgresql-contrib

Now use the following command to start postgresql terminal and start typing postgresql commands which are somewhat similar to MySQL.

$ sudo -u postgres psql postgres

After this a terminal will open up where you put all your commands. If this does not work use the following command.

$ sudo /etc/init.d/postgresql restart

Now use the earlier command again and it will definitely work.

After this I created a table in postgresql using simple create table command.

Now to copy the data of .csv file into postgresql table use the following command.

 postgres=# COPY zip_codes FROM '/path/to/csv/ZIP_CODES.txt' DELIMITER ',' CSV HEADER;

Now you can use the command like select * from table_name; and watch the table on your screen populated with the new data.

After this was done I had to create a graph out of the given data. I searched this on the google and found a good javascript library for doing this.

Here is the link for creating graphs from the input of .csv file.

http://canvasjs.com/forums/topic/graph-a-csv-file/

Now as the .csv file had many comma separated values my friend went forward and asked me to create a script that can create different files with the required data for this I told him that it will require some time. I came back to my room and started making a python script that can keep two comma separated values in each file.

Here is the part that created the first file.

target = open(“stat3112.csv”, ‘r+’)
temp1 = 0
for line in target:
index = line.find(“,”)
resttext = line[index + 1:]
print resttext
index_rest_text = resttext.find(“,”)
net_index = index + index_rest_text + 1

newfile = “file” + str(temp1) + “.csv”
files = open(newfile, ‘a’)
print index_rest_text
files.write(line[:net_index] + “\n”)
files.close()

target.close()

Therefore, The task was completed.

Simple JavaScript program that handle Keyboard events

In this post I am going to talk about one of important aspect in programming i.e. handling keyboard events. Now being important doesn’t mean that it is difficult but I have to admit that I haven’t tried it from the day I had started programming. Now I was working with one of my friend and she wanted me to do it for her. I found the task interesting as I haven’t done it earlier.

Here in this post I am not going to create a simple JavaScript program that will raise an alert whenever any of the arrow key is pressed. The program will detect the arrow key at any point while the browser is working. But before diving in we will discuss the very basics of the keyboard event handling in JavaScript.

There are three basic types of keyboard event Listener in JavaScript that handles this type of event.

  1. keydown
  2. keyup
  3. keypress
  • Keydown stands for the stage when the any key is pressed.
  • Keyup is the exact opposite of keydown
  • Keypress only works when one of the character key is pressed. You might like to work with this when you want to work with something fixed.

After reading this you should have got a clear view that what we want to try is keydown event.

Now lets dive right into the program and write some code.

<!DOCTYPE html>
<html>
  <head>
    <title>Nothing</title>
    
      window.addEventListener("keydown", onkeypress, false);
      function onkeypress(key){
        if(key.keyCode == "37"){
          alert("Left key is pressed");
        }
        if(key.keyCode == "38"){
          alert("Up key is pressed");
        }
        if(key.keyCode == "39"){
          alert("Right key is pressed");
        }
        if(key.keyCode == "40"){
          alert("Down key is pressed");
        }
      }
    
  </head>
  <body>
  </body>
</html>

Now the coding part is pretty straight forward but still if you find something odd about this program please drop a comment and I will get back to you.

Using my own coded software to study for Exams

Some time ago sir hosted a hackathon to make some progress to enhance the ebook software that he was using on his website. Everyone was given a specific task on that day. My task was to read the script and make some changes in the script. But some reason we had to drop that idea. So I decided to start building something new instead.

I wrote some code and was able to create a replica of the software( Although It was not as great as that software). The code of the software can be found here.

Due to dropping of the idea the task become much more complex and we are still figuring it out. But I showed the new code written by me to sir and sir asked me to test it for the books of my choice. I was not able to test it because I don’t read much books unless it is very important (although I love to read books).

So, the time came when it become very important to read as it was the Exam time. I was not having the notes of the subject so I went to one of my friend and asked him to give me the notes. As he was also studying so I decided not to disturb him and take the pictures of the notebook. I used my phone for this purpose. I started studying the pictures from the phone and soon enough become fed up because of the small screen then I decided to move the pictures to my laptop. I started studying but before the completion of the page it become difficult for me concentrate.

Then I decided to use the software that I have coded some time ago. First of all I decided to change the name of the pictures as for the pictures to get imported we have to change the name and bring the images to the png format and It was a large task to change the name of each and every file so I looked for some python script on the internet. Soon enough I found something that was explained enough so that I can use it. I changed the script according to my needs and was able to successfully change the name of images.

After I saw the images carefully I was able to find that the images were not in the right order so I went to one of my senior(Amarjeet). He told me to create a list first and then send the list to the function which changed the name of the images. Earlier it was a dictionary by default and since the values in the dictionary are in a random order so we were not getting the correct order.

So in the end I solved this problem and took the pictures and put them into the required folder. And in the end I manipulated the JSON file and finally was able to run the image on the browser.

On opening the browser I found that the images were 90 degree toward the left. So, I used this command to rotate it back to normal.

mogrify -rotate “90” *.png