fanout / django-eventstream

Server-Sent Events for Django
MIT License
638 stars 84 forks source link

Support multiline payloads #129

Closed jonhuber closed 9 months ago

jonhuber commented 10 months ago

This adds support for multiline payloads.

For example, in Hotwire Turbo Streams (https://turbo.hotwired.dev/handbook/streams) HTML can be sent as an event and update a portion of a page. Currently sending a string of HTML with newlines will produce the following event.

event: message
data: <turbo-stream action='append' target='messages'>
  <template>
    <div id='message_2'>
      The content of the message.
    </div>
  </template>
</turbo-stream>

However, the browser will only handle the first line of data because the subsequent lines do not have a leading "data:". This PR adds processing to break apart the payload to add "data:" to each line.

event: message
data: <turbo-stream action='append' target='messages'> 
data:   <template> 
data:     <div id='message_2'> 
data:       The content of the message.
data:     </div> 
data:   </template> 
data: </turbo-stream>
jkarneges commented 9 months ago

Thank you @jonhuber!