pygnetic / doc / api / index.rst

Small FAQ

Why I have to register message? Can't I just use dictionary to send it?

:meth:`~.message.MessageFactory.register` creates a compact data structure - :func:`namedtuple <collections.namedtuple>`, which contains only essential data, reducing overall amount of data to send. Take a look at example below and compare sizes of packed dictionary and structure created by :class:`~.message.MessageFactory`.

>>> import msgpack
>>> m1 = msgpack.packb({'action':'chat_msg', 'player':'Tom', 'msg':'Test message'})
>>> m1, len(m1)
('\x83\xa6action\xa8chat_msg\xa6player\xa3Tom\xa3msg\xacTest message', 45)
>>> import pygnetic as net
>>> net.init()
17:11:40 INFO     Using enet_adapter
17:11:40 INFO     Using msgpack_adapter
True
>>> chat_msg = net.register('chat_msg', ('player', 'msg'))
>>> m2 = net.message.message_factory.pack(chat_msg('Tom', 'Test message'))
>>> m2, len(m2)
('\x93\x02\xa3Tom\xacTest message', 19)

The only drawback of this method is the need to register the same messages in the same order in client and server.

Why order of registration messages is important?
As You may noticed in previous example, there is no string with type of message in packed data. That's because type is encoded as integer, depending on order of registration.

Glossary

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.