Archive

Posts Tagged ‘ejabberd’

Erlang meets baseball: welcome TOK.tv!

November 27, 2012 Leave a comment

In this post I would to tell you something about my collaboration for TOK Baseball, the free iPad application created by the startup TOK.tv.

Last summer I was contacted by Fabrizio Capobianco, the CEO and founder of TOK.tv for a possible Erlang freelance job. During our discussion Fabrizio introduced me his idea about a brand new iPad application: TOK Baseball. The concept behind the application was very interesting; in today’s society more and more people are living away from their relatives or childhood friends, loosing the oppurtinity of watching a game togheter, and that is really sad.

If you are a sport fanatic as I am, you know what I mean. I believe that nothing beats the experience of watching a game togheter with your buddies: commenting a bad tackle, complaining your own team bad play or screaming for an amazing victory is really something! Here is where TOK.tv comes in.

TOK Baseball is an iPad application to be used as a second screen while you are watching a baseball game on tv, but unlike many other applications it doesn’t consist on a really boring number of Twitter status updates about the match. TOK Baseball allows you to invite up to three friends and have a real voice conversation while the app pushes live stats about the game you are watching. The application is not intended to substitute your tv, it’s rather a good way to enrich your experience, a way to bring back the concept of sociality in your tv/sport routine.

At this point, I may hear many of you saying: “Ok Paolo, nice ad for the application…but how is this related at all with Erlang?“. Well, Fabrizio and the other guys at TOK.tv are nice people so they let me write a little bit about my collaboration with them in my blog. First of all you may be interested in knowing that Erlang plays a major role in the application backend: the social core of the app is actually implemented using as base eJabberd, the very famous XMPP server you should already have heard of.

My work for TOK.tv was in fact devoted to the implementation of some eJabberd internal components, mostly related to the concept of friendship. I have to say that I did my last “real” work with XMPP during my bachelor thesis experience, so it was really good to go back and put my hands on eJabberd and Erlang stuff.

eJabberd code can be really hard to grasp since there is much code to go through and most of the times you have to jump from file to file, but once you start knowing how it works and its basic principles you can really enjoy it and undestand why it is still one of the most famous and appreciated Erlang applications out there.

Was it a good idea to use Erlang and eJabberd for the application backend? In my humble opinion it was indeed. Just think about this: for each game, you can have a huge number of users logging in and out, moving from match to match, making friendships and inviting friends…all of this must be very reliable and fast, especially because it must be connected to the real time voice experience. That’s way I strongly believe that by using Erlang the TOK.tv crew made a sound decision.

One more think: TOK.tv development is everything but slow! Soon the company will be launching TOK Football, the perfect companion for all the football fanatic….you know what? I hope they will have the application for Italian soccer soon enough!

Categories: Erlang Tags: , , , , ,

An interview with Michal Ptaszek (@michalptaszek)

June 3, 2011 Leave a comment

This is one of the last interviews I made to the speakers of  Erlang Factory London 2011. One of my favourite erlang developers is Michal Ptaszek. Michal works at Erlang Solutions Ltd in Krakow. He is pretty famous among ejabberd developers and XMPP lovers.

Ask and Aswer

Paolo - Michal, can you introduce yourself to our readers?
Michal - Hello, I am Michal Ptaszek – Erlang passionate and evangelist, someone who became seduced by the beauty of logic of maths and algorithms in his early childhood. Currently I work for Erlang Solutions, trying to cope with the complexity of massive scale Internet Messaging systems. From personal perspective, I have done my masters in Krakow University of Science and Technology in Grid systems field (Erlang obviously!), currently I live in Krakow, Poland, love jazz and modern literature (especially latin). Some day would like to find some time to learn how to play sax.

Paolo - How did you “discover” Erlang?
Michal - It has started in the early 2008, when Joe Armstrong came to Poland to give a talk on Erlang concepts for Student’s IT Festival. Of course almost no one has ever heard of the language (or. even if, never touched it), and as Joe is such a great speaker, he planted some seeds of curiosity in my mind.

Paolo - How long did it take to you to master Erlang?
Michal - I wouldn’t say I’ve mastered Erlang. Comparing to such people as Ulf Wiger or Robert Virding, I’m still in my infancy :) The great thing in Erlang is that you do not have tons of code everywhere: language is simple, standard libraries are manageable, even VM: if you clench your fists you can read and understand the entire code in few weeks.
In my opinion people are in never-ending continuos process of developing themselves: when I look at my code written a year/two ago I feel ashamed of myself and would love to throw it away and rewrite it in some nicer way. I guess at the time when I discover there is nothing to improve I will be forced to change the profession (or at least the programming language) ;)

Paolo - How did you find your first Erlang related job?
Michal - Few months later after Joe’s visit to Poland, Erlang Solutions (Erlang Training and Consulting back then) decided to open an office in Krakow: I did not hesitate a lot and applied for the internships. The office grew from 3 to 12 people since then and is extending all the time: I guess it was a really good choice.

Paolo - What are the things you love most of Erlang?
Michal - Just to name two most important things:
– built-in parallelism and distribution – I can not even imagine how do people distribute their applications and protect their critical sections using semaphores and mutexes. It’s so 1990…
– fault tolerance – one of the least appreciated things in the language: ability to isolate the errors, propagate them in the controlled way and recover automatically. In the systems that run on top of tens/hundreds of machines crash of one of them is not a question of ‘if’ but ‘when’.

Paolo - During Erlang Factory, you will talk about Erlang and XMPP: can you give to our readers a brief description of your course?
Michal - Yes, together with Michal Slaski we are going to teach a one day course on Erlang and XMPP (ejabberd in particular). During those few hours we would like to show how the things work under ejabberd’s hood, why it is not a best idea to keep the default configuration, what are the
low hanging fruits for ejabberd, BEAM and OS enhancements, how can we monitor existing server, how to cluster several daemons together in one command and finally: what are the ways to extend the functionality programmatically.

Paolo - Who should follow your course and why?
Michal - Definitely people who are interested in Internet Messaging, even not necessarily in XMPP. Architecture of message/presence router, as well as the easiness of extending current functionality, federating with other protocols or finally scalability might be stimulative to someone who
plans to set up a startup or replace his current messaging server with something better.

Paolo - Why should an XMPP programmer use Erlang as development language?
Michal - Mostly because of the concurrency and scalability. Erlang actor process model fits perfectly in the concept of independent user sessions and communicating with each other using asynchronous message passing.

Paolo - In your opinion, what is the most important thing to keep in mind when developing with Erlang and XMPP?
Michal - Global state/shared data is a root of all evil. The less things you share – the better you can scale. XMPP systems have tendencies to grow rapidly: it’s basically the linear relation to the number of simultaneously connected users. Shared global state will not pop up as a bottleneck until it is too late: when users are storming our gates and we realize that things should have been done in a different way long time ago.
And there is also one more rule, that people often forget about: KISS. IM systems are really simple, let’s do not overengineer them

Paolo - Can you recommend to our readers some books, sites, or blogs that will help them in the world of Erlang and XMPP?
Michal - Definitely take a look at Jack Moffitt’s both book (“Professional XMPP Programming”) and webiste (http://metajack.im/). Another worth-reading blog is Stefan Strigler’s http://blog.jwchat.org/. To be honest: there is still no book on Erlang and XMPP in particular. Maybe it is a good time to change it? :)

How to handle presence stanzas using EXMPP

August 10, 2010 Leave a comment

I have written many posts this year about EXMPP and XMPP; we discussed about message stanzas and IQ stanzas therefore I would like to tell you something about presence handling today, so that you would be able to start some implementation by yourselves.

Presence in XMPP is used to communicate to other users our status (e.g “online”, “do not disturb”, “away”, etc etc); let’s see how we can improve our component by exploiting this kind of stanzas.

In a previous post we saw how to handle user subscription, anyhow we didn’t take in consideration the fact that many times one user can be connected to the server with a status different than “online”…we don’t want our bot to become a spamming machine, users should receive notifications only if they are using an “online” status.

How can we achieve this?

Ok, first of all our bot should subscribe to user’s presence during the registration process, this way we could understand whether to send the notifications or not; we can do this by using the following functions:

subscribe_to_presence(JID) ->
    CompAddr = ?XMLCDATA(<>),
    Presence = ?XMLEL4(?NS_USER_NICKNAME, 'nick', [], [CompAddr]),
    exmpp_xml:append_child(make_presence(JID, <<"subscribe">>), Presence).

make_presence(JID, Type) ->
    From = ?XMLATTR('from', ?COMPONENT_ADDRESS),
    PresenceType = ?XMLATTR('type', Type),
    Presence = ?XMLEL4(?NS_COMPONENT_ACCEPT, 'presence', [PresenceType, From], []),
    exmpp_stanza:set_recipient(Presence, JID).

so that we can send to the user the following stanzas:

<presence from="mod_xxxxx.localhost" 
  type="subscribe" to="user@localhost" 
  id="Component-985548963" >
<nick xmlns="http://jabber.org/protocol/nick">mod_xxxxx.localhost</nick>
</presence>

If you are using PSI, you will receive this:

Component asks for presence subscription

Component asks for presence subscription

 

In the previous code, ?COMPONENT_ADDRESS is nothing more than the address of our component (e.g. mod_xxxxx.domain), it is just the address to which the user should send his status updates; at this point if the user sends back a presence stanza with the attribute type set to “subscribed” the process is successful, and in you PSI your user will have a new Agents/Transports contact in his roster.

Component in now in user's roster

Component in now in user's roster

 

Now one can say: “Hey! Wait a second! What are ?XMLCDATA, ?XMLEL4, ?XMLATTR and ?NS_USER_NICKNAME?”, well let’s say that in previous posts I didn’t tell you all the truth: if you include in your module the library exmpp_xml.hrl with:

-include_lib("exmpp/include/exmpp_xml.hrl").

you will be able to build xml elements and xmlcdata with this easier sintax :) , I suggest you to take a look at that file, so that you will see all the other functions provided.

Another smart thing to do is to include the library exmpp_nss.hrl:

-include_lib("exmpp/include/exmpp_nss.hrl").

this way you will be able to use a set of macros for the different namespaces, instead of declaring them by yourselves.

Now what? Well, we should process the presence stanzas as we did for messages and IQs; as I told you before I want to send notifications only to user whose status is set to “online”, thus we should upgrade our mnesia user_table whenever a presence stanza is received, we can do it using the following code:

process_received_presence(_Session,
	                  #received_packet{packet_type=presence,
                          type_attr=_Type, raw_packet=Presence}) ->
    Status = exmpp_presence:get_show(Presence),
    From = exmpp_jid:parse(exmpp_stanza:get_sender(Presence)),
    BareFrom = binary_to_list(exmpp_jid:prep_bare_to_binary(From)),

    case get_subscription(BareFrom) of
	{subscribed, UId, Pwd} ->
	    mnesia:dirty_write({user_table, BareFrom, Status, UId, Pwd});
	_ ->
	    nothing
    end.

And this is done! Now we can select all the users whose status in online using:

mnesia:dirty_index_read(user_table, online, presence).

We can use this function because we set the field presence as a secondary index while creating the Mnesia scheme.

And for today this is all!

Follow

Get every new post delivered to your Inbox.

Join 27 other followers