Warning: Bocadillo is now UNMAINTAINED. Users are recommended to migrate to a supported alternative, such as Starlette or FastAPI. Please see #344 for more information.

bocadillo.sse

server_event

server_event(self, name:str=None, *, data:Sequence=None, json:Any=None, id:int=None)

A string-like object that represents a Server-Sent Event.

Examples

>>> from bocadillo import server_event
# Empty event
>>> server_event()
'\n\n'
# Event with data
>>> server_event("hello, world")
'data: hello, world\n\n'
# Named event with JSON data
>>> server_event(name="userconnect", json={"username": "bobby"})
'event: userconnect\ndata: {"username": "bobby"}\n\n'

Parameters

  • name (str): An optional name for the event.
  • data (str or sequence): The event data. A sequence of strings can be given for multi-line event data.
  • json (any): A JSON-serializable value which, if given, is serialized and used as data.
  • id (int): An optional id for the event.