File: namespace.rst

package info (click to toggle)
gevent-socketio 0.3.6-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 716 kB
  • ctags: 551
  • sloc: python: 2,031; makefile: 134
file content (236 lines) | stat: -rw-r--r-- 8,244 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
.. _namespace_module:

:mod:`socketio.namespace`
=========================

.. automodule:: socketio.namespace

.. autoclass:: BaseNamespace


Namespace initialization
------------------------

     You can override this method:

     .. automethod:: BaseNamespace.initialize

Event flow
----------

This is an attempt at catching the gotchas of the Socket.IO protocol,
which, for historical reasons, sometimes have weird event flow.

The first function to fire is ``initialize()``, which will be called
only if there is an incoming packet for the Namespace.  A successful
javascript call to ``io.connect()`` **is not** sufficient for
``gevent-socketio`` to trigger the creation of a Namespace object.
Some event has to flow from the client to the server.  The connection
will appear to have succeeded from the client's side, but that is
because ``gevent-socketio`` maintains the virtual socket up and running
before it hits your application.  This is why it is a good pratice to
send a packet (often a ``login``, or ``subscribe`` or ``connect`` JSON
event, with ``io.emit()`` in the browser).

If you're using the GLOBAL_NS, the ``recv_connect()`` will not fire on
your namespace, because when the connection is opened, there is no
such packet sent.  The ``connect`` packet is only sent over (and
explicitly sent) by the javascript client when it tries to communicate
with some "non-global" namespaces.  That is why it is recommended to
always use namespaces, to avoid having a different behavior for your
different namespaces. It also makes things explicit in your
application, when you have something such as ``/chat``, or
``/live_data``.  Before a certain version of Socket.IO, there was only
a global namespace, and so this behavior was kept for backwards
compatibility.

Then flows the normal events, back and forth as described elsewhere (elsewhere??).

Upon disconnection, here is what happens: [INSERT HERE the details
flow of disconnection handling, events fired, physical closing of the
connection and ways to terminate a socket, when is the Namespace
killed, the state of the spawn'd processes for each Namespace and each
virtsocket. This really needs to be done, and I'd appreciate having
people investigate this thoroughly]

There you go :)


Namespace instance properties
-----------------------------

     .. attribute:: BaseNamespace.session

       The :term:`session` is a simple ``dict`` that is created with
       each :class:`~socketio.virtsocket.Socket` instance, and is
       copied to each Namespace created under it.  It is a general
       purpose store for any data you want to associated with an open
       :class:`~socketio.virtsocket.Socket`.

     .. attribute:: BaseNamespace.request

       This is the ``request`` object (or really, any object) that you
       have passed as the ``request`` parameter to the
       :func:`~socketio.socketio_manage` function.

     .. attribute:: BaseNamespace.ns_name

       The name of the namespace, like ``/chat`` or the empty string,
       for the "global" namespace.

     .. attribute:: BaseNamespace.environ

       The ``environ`` WSGI dictionary, as it was received upon
       reception of the **first** request that established the virtual
       Socket.  This will never contain the subsequent ``environ`` for
       the next polling, so beware when using cookie-based sessions
       (like Beaker).

     .. attribute:: BaseNamespace.socket

       A reference to the :class:`~socketio.virtsocket.Socket`
       instance this namespace is attached to.

Sending data
------------

     Functions to send data through the socket:

     .. automethod:: BaseNamespace.emit

     .. automethod:: BaseNamespace.send

     .. automethod:: BaseNamespace.error

     .. automethod:: BaseNamespace.disconnect


Dealing with incoming data
--------------------------

     .. automethod:: BaseNamespace.recv_connect

     .. automethod:: BaseNamespace.recv_message

     .. automethod:: BaseNamespace.recv_json

     .. automethod:: BaseNamespace.recv_error

     .. automethod:: BaseNamespace.recv_disconnect

     .. method:: BaseNamespace.exception_handler_decorator(fn)

       This method can be a static, class or bound method (that is, with
       ``@staticmethod``, ``@classmethod`` or without).  It receives one
       single parameter, and that parameter will be the function the
       framework is trying to call because some information arrived from
       the remote client, for instance: ``on_*`` and ``recv_*``
       functions that you declared on your namespace.

       The decorator is also used to wrap called to
       ``self.spawn(self.job_something)``, so that if anything happens
       after you've spawn'd a greenlet, it will still catch it and
       handle it.

       It should return a decorator with exception handling properly
       dealt with.  For example:

       .. code-block:: python

        import traceback, sys
        import logging
        def exception_handler_decorator(self, fn):
            def wrap(*args, **kwargs):
                try:
                    return fn(*args, **kwargs)
                except Exception, e:
                    stack = traceback.format_exception(*sys.exc_info())
                    db.Evtrack.write("socketio_exception",
                                     {"error": str(e),
                                      "trace": stack},
                                     self.request.email)
                    logging.getLogger('exc_logger').exception(e)
            return wrap


     .. automethod:: BaseNamespace.process_event

     You would override this method only if you are not completely
     satisfied with the automatic dispatching to ``on_``-prefixed
     methods.  You could then implement your own dispatch.  See the
     source code for inspiration.


Process management
------------------

     Managing the different callbacks, greenlets and tasks you spawn from
     this namespace:

     .. automethod:: BaseNamespace.spawn

     .. automethod:: BaseNamespace.kill_local_jobs

ACL system
----------

     The ACL system grants access to the different ``on_*()`` and
     ``recv_*()`` methods of your subclass.

     Developers will normally override :meth:`get_initial_acl` to
     return a list of the functions they want to initially open.
     Usually, it will be an ``on_connect`` event handler, that will
     perform authentication and/or authorization, set some variables
     on the Namespace, and then open up the rest of the Namespace
     using :meth:`lift_acl_restrictions` or more granularly with
     :meth:`add_acl_method` and :meth:`del_acl_method`.  It is also
     possible to check these things inside :meth:`initialize` when,
     for example, you have authenticated a Global Namespace object,
     and you want to re-use those credentials or authentication infos
     in a new Namespace:

     .. code-block:: python

         # GLOBAL_NS = ''

         class MyNamespace(BaseNamespace):
             ...
             def initialize(self):
                 self.my_auth = MyAuthObjet()
                 if self.socket[GLOBAL_NS].my_auth.logged_in == True:
                     self.my_auth.logged_in = True

     The content of the ACL is a list of strings corresponding to the full name
     of the methods defined on your subclass, like: ``"on_my_event"`` or
     ``"recv_json"``.

     .. automethod:: BaseNamespace.get_initial_acl

     .. automethod:: BaseNamespace.add_acl_method

     .. automethod:: BaseNamespace.del_acl_method

     .. automethod:: BaseNamespace.lift_acl_restrictions

     .. automethod:: BaseNamespace.reset_acl

     This function is used internally, but can be useful to the developer:

     .. automethod:: is_method_allowed

     This is the attribute where the allowed methods are stored, as a list of
     strings, or a single ``None``::

     .. autoattribute:: allowed_methods

Low-level methods
-----------------

     Packet dispatching methods. These functions are normally not overriden if
     you are satisfied with the normal dispatch behavior:

     .. automethod:: BaseNamespace.process_packet

     .. automethod:: BaseNamespace.call_method_with_acl

     .. automethod:: BaseNamespace.call_method