Python API: Emitting Messages¶
-
fedmsg.publish(*args, **kw)¶ Send a message over the publishing zeromq socket.
>>> import fedmsg >>> fedmsg.publish(topic='testing', modname='test', msg={ ... 'test': "Hello World", ... })
The above snippet will send the message
'{test: "Hello World"}'over the<topic_prefix>.dev.test.testingtopic. The fully qualified topic of a message is constructed out of the following pieces:This function (and other API functions) do a little bit more heavy lifting than they let on. If the “zeromq context” is not yet initialized,
fedmsg.init()is called to construct it and store it asfedmsg.__local.__contextbefore anything else is done.An example from Fedora Tagger – SQLAlchemy encoding
Here’s an example from fedora-tagger that sends the information about a new tag over
org.fedoraproject.{dev,stg,prod}.fedoratagger.tag.update:>>> import fedmsg >>> fedmsg.publish(topic='tag.update', msg={ ... 'user': user, ... 'tag': tag, ... })
Note that the tag and user objects are SQLAlchemy objects defined by tagger. They both have
.__json__()methods whichfedmsg.publish()uses to encode both objects as stringified JSON for you. Under the hood, specifically,.publishusesfedmsg.encodingto do this.fedmsghas also guessed the module name (modname) of it’s caller and inserted it into the topic for you. The code from which we stole the above snippet lives infedoratagger.controllers.root.fedmsgfigured that out and stripped it down to justfedorataggerfor the final topic oforg.fedoraproject.{dev,stg,prod}.fedoratagger.tag.update.Shell Usage
You could also use the
fedmsg-loggerfrom a shell script like so:$ echo "Hello, world." | fedmsg-logger --topic testing $ echo '{"foo": "bar"}' | fedmsg-logger --json-inputParameters: - topic (unicode) – The message topic suffix. This suffix is joined to the
configured topic prefix (e.g.
org.fedoraproject), environment (e.g.prod,dev, etc.), and modname. - msg (dict) – A message to publish. This message will be JSON-encoded prior to being sent, so the object must be composed of JSON- serializable data types. Please note that if this is already a string JSON serialization will be applied to that string.
- modname (unicode) – The module name that is publishing the message. If this
is omitted,
fedmsgwill try to guess the name of the module that called it and use that to produce an intelligent topic. Specifyingmodnameexplicitly overrides this behavior. - pre_fire_hook (function) – A callable that will be called with a single argument – the dict of the constructed message – just before it is handed off to ZeroMQ for publication.
- topic (unicode) – The message topic suffix. This suffix is joined to the
configured topic prefix (e.g.