Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-13591

SseEmitter cannot use "send(SseEventBuilder builder)" method with custom SseEventBuilder implementation

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: 4.2.1
    • Fix Version/s: 4.2.3
    • Component/s: None
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      SseEmitter class contains method "send(SseEventBuilder builder)" which is supposed to be used for custom messages. But the first line of this method contains code

      Set<DataWithMediaType> dataToSend = ((SseEventBuilderImpl) builder).build();
      

      That means if we pass SseEventBuilder to the method we receive a ClassCastException since we cannot cast our implementation of interface SseEventBuilder to built-in implementation SseEventBuilderImpl. And SseEventBuilderImpl is a private class, so we will not be able to override it.
      So basically that means we cannot create our custom messages at all.

        Activity

        Hide
        rstoya05-aop Rossen Stoyanchev added a comment -

        I've removed the class cast and added a build method to SseEventBuilder.

        By the way I'd interested to hear more about your use case with creating a custom message, i.e. the underlying reason for wanting to extend SseEventBuilder.

        Show
        rstoya05-aop Rossen Stoyanchev added a comment - I've removed the class cast and added a build method to SseEventBuilder. By the way I'd interested to hear more about your use case with creating a custom message, i.e. the underlying reason for wanting to extend SseEventBuilder.
        Hide
        mshalak Maksym Shalak added a comment -

        We just wanted to get rid of the "data:" prefix and pass pure JSON into the stream.

        Show
        mshalak Maksym Shalak added a comment - We just wanted to get rid of the "data:" prefix and pass pure JSON into the stream.
        Hide
        rstoya05-aop Rossen Stoyanchev added a comment - - edited

        Is this for Server-Sent Events you mean? According to the spec the field name should be: event, data, id, retry, or otherwise should be ignored. Or if you just want to stream data to the response (not necessarily SSE-formatted) why not use ResponseBodyEmitter instead of SseEmitter?

        Show
        rstoya05-aop Rossen Stoyanchev added a comment - - edited Is this for Server-Sent Events you mean? According to the spec the field name should be : event, data, id, retry, or otherwise should be ignored. Or if you just want to stream data to the response (not necessarily SSE-formatted) why not use ResponseBodyEmitter instead of SseEmitter?
        Hide
        mshalak Maksym Shalak added a comment -

        Yep, looks like ResponseBodyEmitter better suites my needs, thanks.

        Show
        mshalak Maksym Shalak added a comment - Yep, looks like ResponseBodyEmitter better suites my needs, thanks.

          People

          • Assignee:
            rstoya05-aop Rossen Stoyanchev
            Reporter:
            mshalak Maksym Shalak
            Last updater:
            St├ęphane Nicoll
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Days since last comment:
              2 years, 17 weeks, 3 days ago