How can a site post events ...

    Once upon a time (I don’t remember when, but long ago) I packed up and launched a website for myself. At first it was used simply as a warehouse of what you need to convey to someone. Then, quite by chance, free time formed and it turned out to fill the site with something meaningful and (I hope) useful. And I really wanted to keep abreast of the events taking place on the site. The simplest solution is to send letters, which was done. However, after a while there was a desire to get rid of letters from the site since the flow of office mail during the day exceeds reasonable limits.

    And then came up with the idea that using mail as a transport for delivering site events is just one of the common approaches, however there are transports that allow you to not only send messages almost instantly, but also track the status of the recipient and guarantee the delivery of the event. One of these transports is jabber.

    A bit of reasoning before rushing into implementation.


    So, at the moment, the server sent me letters at any time:
    1. Suggestions / comments that can be sent from the site
    2. Information about downloading files from the site
    3. Regularly small statistics

    I didn’t need to deliver all of these events at any time: I need information about downloading files from the site as the fact that “right now someone is taking the file from the site”, and not to pick up a few letters in the morning after receiving the mail. It would be more interesting to get statistics on demand (yes, you can just make a page, however, such pages usually turn into monsters, but you need to see something like summary). Thus, sending events should turn out like this:
    1. Suggestions / comments that can be sent from the site - always sent
    2. Information about downloading files from the site - is sent only when I'm on-line
    3. Regularly small statistics - when I wanted to look at it

    By the way, my IM client starts automatically and much earlier than the mail client i.e. site events I will see faster.

    Preparatory work.


    To get started, I got a jabber ID under which my site will work. I did not bother with the site supporting authorization requests, I did all the necessary steps to add contacts and authorization manually. this is just an experiment. In the future, you need to bind registered users to the server contact list.
    The second most important thing was choosing a library for working with the jabber protocol. There were a sufficient number of projects for .NET on the network, but I have not yet made the final choice of a specific library since still continue their comparison.

    We make the client from the jabber site.


    Everything is ready for implementation and the first code that was written just logged into the jabber server and appeared in my online contacts when the site started. The first problem that threatened to stop the experiment: for some reason, some libraries believe that they are simply obliged to ask the user for confirmation. Naturally, the site could not answer anything to them, and did not allow creating a simple window. After a simple replacement of the library for working with the protocol, I finally saw the long-awaited inscription "[Server] On-Line" Then I got excited - this problem met me in a completely different library - the library for working with SVN, which I also attached to the site.

    We start sending events.


    Actually, the whole development consisted of writing the void SendMessage method (Jid to, bool alwayssend, string format, params object [] args) and placing calls to this method. I also wrote a small method for auto answer:
    1.     protected void processMessage(Jid jid, string text)
    2.     {
    3.       string cmd      = new Regex("\\s+", RegexOptions.ECMAScript).Replace(text, " ").ToLower();
    4.       switch (cmd)
    5.       {
    6.         case "?":
    7.           SendMessage(jid, false, "Команды:\n"+
    8.                       "как дела? - текущая статистика\n"+
    9.                       "посетители? - список IP посетителей\n"+
    10.                       "страницы? - список страниц\n"+
    11.                       "откуда приходили? - список ссылающихся серверов.");
    12.           break;
    13.         case "как дела?":
    14.           SendMessage(jid, false, Collector.GetSummary());
    15.           break;
    16.         case "посетители?":
    17.           SendMessage(jid, false, Collector.GetIPs());
    18.           break;
    19.         case "страницы?":
    20.           SendMessage(jid, false, Collector.GetPages());
    21.           break;
    22.         case "откуда приходили?":
    23.           SendMessage(jid, false, Collector.GetServers());
    24.           break;
    25.       }
    26.     }
    * This source code was highlighted with Source Code Highlighter.

    That's it, now I see when my site is working, sends me the necessary events and answers questions. This is what the chat window looks like with my site:
    image
    Actually, writing an answering machine is easy.

    The results.


    Using the jabber protocol as a transport for delivering site events is convenient and uncomplicated. You can immediately see the advantage - all events can be divided into several types and linked to the presence of a user (in this case, me) in touch and (if necessary) to my status (do not disturb, busy, etc.):
    • Important Messages - What I Must See
    • Events that are of interest, but when I'm off-line or my status indicates my workload, then you can not send them
    • Information sent on request - only when I wanted to look at it, i.e. it generally looks like a command line :)

    The number of “like” necessary letters has been reduced, which in 99% of cases are deleted without reading.
    In general, it turned out that the potential of such a solution is quite high. For example, you can go further - radically change the registration process: ask only jabber ID, send an authorization request, pull out the initial data and send the password, but so far the low prevalence of using the protocol stops it.
    The implementation of the test mechanism on the server fit in 134 lines of code, if anyone is interested, I can lay out, but this code is almost completely taken from the examples that came with the library. As long as I like the results, the experiment continues. Ahead - load testing, tight work with a list of contacts through the site and a bunch of ideas.

    Addition: about components for working with jabber

    The first worthy project is Jabber .NET : since mail and jabber for my domain work through Google Apps, the main criterion is the normal work with google, the connection work without problems. There are some flaws, but the basic functions are provided quite well. What didn’t suit me:
    1. Inheriting Objects from System.ComponentModel.Component
    2. Multiple dlls
    3. The structure of the project is completely incomprehensible to me, the projects are too mixed


    The second project is agsxmpp : at the moment I use it, but most likely I will have to refuse because of the license. Again, it works fine with google, the library is more serious than the first.

    Both considered libraries contain components for Windows.Forms, but firstly you cannot look at them without a shudder, and secondly they are unnecessary to me on the server.

    Everything else that I could find was swept aside already at the stage of studying sites. However, there are some good JScript projects, maybe some of them can simply be ported to C #.

    Also popular now: