slixmpp/docs/getting_started/component.rst

76 lines
2.5 KiB
ReStructuredText
Raw Normal View History

2011-12-31 07:00:54 +00:00
.. _echocomponent:
=================================
Create and Run a Server Component
=================================
2011-12-31 07:00:54 +00:00
.. note::
2014-08-17 19:53:34 +00:00
2011-12-31 07:00:54 +00:00
If you have any issues working through this quickstart guide
or the other tutorials here, please either send a message to the
2014-07-17 12:19:04 +00:00
`mailing list <http://groups.google.com/group/slixmpp-discussion>`_
2011-12-31 07:00:54 +00:00
or join the chat room at `sleek@conference.jabber.org
<xmpp:sleek@conference.jabber.org?join>`_.
2014-07-17 12:19:04 +00:00
If you have not yet installed Slixmpp, do so now by either checking out a version
from `Github <http://github.com/fritzy/Slixmpp>`_, or installing it using ``pip``
2011-12-31 07:00:54 +00:00
or ``easy_install``.
.. code-block:: sh
2014-07-17 12:19:04 +00:00
pip install slixmpp # Or: easy_install slixmpp
2011-12-31 07:00:54 +00:00
2014-08-17 19:53:34 +00:00
Many XMPP applications eventually graduate to requiring to run as a server
component in order to meet scalability requirements. To demonstrate how to
2011-12-31 07:00:54 +00:00
turn an XMPP client bot into a component, we'll turn the echobot example
(:ref:`echobot`) into a component version.
The first difference is that we will add an additional import statement:
.. code-block:: python
2014-07-17 12:19:04 +00:00
from slixmpp.componentxmpp import ComponentXMPP
2011-12-31 07:00:54 +00:00
Likewise, we will change the bot's class definition to match:
.. code-block:: python
class EchoComponent(ComponentXMPP):
def __init__(self, jid, secret, server, port):
ComponentXMPP.__init__(self, jid, secret, server, port)
A component instance requires two extra parameters compared to a client
instance: ``server`` and ``port``. These specifiy the name and port of
the XMPP server that will be accepting the component. For example, for
a MUC component, the following could be used:
.. code-block:: python
2014-07-17 12:19:04 +00:00
muc = ComponentXMPP('muc.slixmpp.com', '******', 'slixmpp.com', 5555)
2011-12-31 07:00:54 +00:00
.. note::
The ``server`` value is **NOT** derived from the provided JID for the
component, unlike with client connections.
One difference with the component version is that we do not have
to handle the :term:`session_start` event if we don't wish to deal
with presence.
The other, main difference with components is that the
``'from'`` value for every stanza must be explicitly set, since
components may send stanzas from multiple JIDs. To do so,
2014-07-17 12:19:04 +00:00
the :meth:`~slixmpp.basexmpp.BaseXMPP.send_message()` and
:meth:`~slixmpp.basexmpp.BaseXMPP.send_presence()` accept the parameters
2011-12-31 07:00:54 +00:00
``mfrom`` and ``pfrom``, respectively. For any method that uses
2014-07-17 12:19:04 +00:00
:class:`~slixmpp.stanza.iq.Iq` stanzas, ``ifrom`` may be used.
2011-12-31 07:00:54 +00:00
Final Product
-------------
.. include:: ../../examples/echo_component.py
:literal: