Skip to main content

Flash messages

Components to display small messages to users.

Sending a message to the current user

To send a message to the current user, you can use the session-based message source. Let’s set one up:

>>> from z3c.flashmessage.sources import SessionMessageSource
>>> from __future__ import unicode_literals
>>> source = SessionMessageSource()
>>> source.send('The world will come to an end in 40 seconds!')

The source allows to list all current messages:

>>> m = list(source.list())
>>> m
[<z3c.flashmessage.message.Message object at 0x...>]
>>> m[0].message
'The world will come to an end in 40 seconds!'
>>> str(m[0].type)
'message'

Receiving messages

The standard message that is generated removes itself from the source when it is received. The receiver will call prepare() on the message before it is handed out to the code that receives it:

>>> m[0].prepare(source)
>>> list(source.list())
[]

There also is another default message that does not delete itself when being read:

>>> from z3c.flashmessage.message import PersistentMessage
>>> source.send(PersistentMessage('I will stay forever!'))
>>> m = list(source.list())[0]
>>> m.message
'I will stay forever!'
>>> m.prepare(source)
>>> list(source.list())
[<z3c.flashmessage.message.PersistentMessage object at 0x...>]

Global receiver

There is a global receiver that queries all message sources that are set up as utilities. Let’s set up a session message source as a utility:

>>> from zope.component import provideUtility
>>> provideUtility(source)
>>> source.send('Test!')
>>> from z3c.flashmessage.sources import RAMMessageSource
>>> source2 = RAMMessageSource()
>>> provideUtility(source2, name='other')
>>> source2.send('Test 2!')
>>> source2.send('Test 3!')
>>> from z3c.flashmessage.receiver import GlobalMessageReceiver
>>> receiver = GlobalMessageReceiver()
>>> m = list(receiver.receive())
>>> len(m)
4
>>> m[0].message
'I will stay forever!'
>>> m[1].message
'Test!'
>>> m[2].message
'Test 2!'
>>> m[3].message
'Test 3!'

After the receiver handed out the messages, they are gone from the sources, because the receiver notifies the messages that they were read:

>>> len(list(receiver.receive()))
1

Filtering message types

When listing messages from a message source, we can restrict which messages we see. If we don’t give a type, then all messages are returned. The default type of a message is message:

>>> source3 = RAMMessageSource()
>>> source3.send('Test 2!')
>>> list(source3.list())
[<z3c.flashmessage.message.Message object at 0x...>]
>>> list(source3.list('message'))
[<z3c.flashmessage.message.Message object at 0x...>]
>>> list(source3.list('somethingelse'))
[]

Performance and Scalability Issues

By default, messages are stored persistently in the ZODB using zope.session. This can be a significant scalability problem; see design.txt in zope.session for more information. You should think twice before using flashmessages for unauthenticated users, as this can easily lead to unnecessary database growth on anonymous page views, and conflict errors under heavy load.

One solution is to configure your system to store flashmessages in RAM. You would do this by configuring a utility providing z3c.flashmessage.interfaces.IMessageSource with the factory set to z3c.flashmessage.sources.RAMMessageSource, and a specific name if your application expects one.

RAM storage is much faster and removes the persistence issues described above, but there are two new problems. First, be aware that if your server process restarts for any reason, all unread flashmessages will be lost. Second, if you cluster your application servers using e.g. ZEO, you must also ensure that your load-balancer supports session affinity (so a specific client always hits the same back end server). This somewhat reduces the performance benefits of clustering.

CHANGES

4.0 (2025-04-14)

  • Replace pkg_resources namespace with PEP 420 native namespace.

3.1 (2025-04-03)

  • Add support for Python 3.12, 3.13.

  • Drop support for Python 3.7, 3.8.

3.0 (2023-02-08)

  • Drop support for Python 2.7, 3.4, 3.5, 3.6.

  • Add support for Python 3.8, 3.9, 3.10, 3.11.

  • Ensure all objects have consistent resolution orders.

2.1 (2018-11-12)

  • Claim support for Python 3.6, 3.7, PyPy and PyPy3.

  • Drop support for Python 3.3.

  • Drop support for python setup.py test.

2.0 (2016-08-08)

  • Standardize namespace __init__.

  • Claim compatibility for Python 3.3, 3.4, and 3.5.

1.3 (2010-10-28)

  • SessionMessageSource implicitly created sessions when the client was reading the messages from the source. Changed internal API so reading no longer creates a session when it not yet exists.

1.2 (2010-10-19)

  • Removed test dependency on zope.app.zcmlfiles.

1.1 (2010-10-02)

  • Removed test dependency on zope.app.testing.

1.0 (2007-12-06)

  • Updated dependency to zope.session instead of zope.app.session to get rid of deprecation warnings.

1.0b2 (2007-09-12)

  • Bugfix: When there was more than one message in a source not all messages would be returned by the receiver.

1.0b1 (2007-08-22)

  • Initial public release.

Release files for z3c.flashmessage 4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for z3c.flashmessage 4.0
File Size Uploaded
z3c_flashmessage-4.0.tar.gz 12.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for z3c.flashmessage 4.0
File Interpreter ABI Platform
z3c_flashmessage-4.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.5 kB

Release files / z3c_flashmessage-4.0.tar.gz

Download URL z3c_flashmessage-4.0.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
621f4638a2228fdb9d724b5afd9701e96709674d605660a79565926fa862d7a2
BLAKE2b-256 checksum
How to use checksums
231d33084c6fb272af8d897728a6ef0eec5df6f4efe647817a8336247ab4a529
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / z3c_flashmessage-4.0-py3-none-any.whl

Download URL z3c_flashmessage-4.0-py3-none-any.whl
Size 11.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6adc9d4451ffdcc10856100ad9edaaedf43a7f52ff3daa4927a1c1f53475fdfa
BLAKE2b-256 checksum
How to use checksums
a8b5efc64b06018edd8c8b38589f259051c4896a4c81df3bbe2931364c3cb2eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page