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.testing topic.

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 as fedmsg.__local.__context before anything else is done.

The modname argument will be omitted in most use cases. By default, fedmsg will try to guess the name of the module that called it and use that to produce an intelligent topic. Specifying modname explicitly overrides this behavior.

The fully qualified topic of a message is constructed out of the following pieces:


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 which fedmsg.publish() uses to encode both objects as stringified JSON for you. Under the hood, specifically, .publish uses fedmsg.encoding to do this.

fedmsg has 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 in fedoratagger.controllers.root. fedmsg figured that out and stripped it down to just fedoratagger for the final topic of org.fedoraproject.{dev,stg,prod}.fedoratagger.tag.update.

Shell Usage

You could also use the fedmsg-logger from a shell script like so:

$ echo "Hello, world." | fedmsg-logger --topic testing
$ echo '{"foo": "bar"}' | fedmsg-logger --json-input


Previous topic


Next topic

Python API: Consuming Messages

Edit this document

Go to Python API: Emitting Messages on GitHub.

Use the web interface to fork the repo, edit the file, and send a pull request.

Your changes will be queued for review under project's Pull requests tab on Github.

Fork me on GitHub