Skip to content

NoContent Status Code for server-sent-events #2153

@pjebs

Description

@pjebs

Server-Sent-Events basically stream responses to the client using (I think) chunked encoding.

The data looks like this:

event: userconnect
data: {"username": "bobby", "time": "02:33:48"}

<1 sec later>

event: usermessage
data: {"username": "bobby", "time": "02:34:11", "text": "Hi everyone."}

<1 sec later>

event: userdisconnect
data: {"username": "bobby", "time": "02:34:23"}

If the connection disconnects, the browser will automatically attempt to reconnect. That is part of the protocol.

For a server to close a connection, it must send a 204 No Content with empty body: https://javascript.info/server-sent-events
This signals to the browser to not attempt to reconnect.

Then the server must disconnect the client.

When we added the Early Hints:

func (ctx *RequestCtx) EarlyHints() error {
...
		c := acquireWriter(ctx)
		defer releaseWriter(ctx.s, c)
		_, err := c.Write(strEarlyHints) // []byte("HTTP/1.1 103 Early Hints\r\n")
		if err != nil {
			return err
		}
		
		_, err = c.Write(strCRLF)
		if err != nil {
			return err
		}
		err = c.Flush()
		if err != nil {
			return err
		}
	}
	return nil
}

Can we add the same functionality or a function to add a status code?

Also, is there way to disconnect?

When I try to send a Header: Connection: close it displays race condition warnings and then panics.
When I try to hijack,

fasthttpRequestCtx.Conn().Close()

it seems to panic on a nil pointer (perhaps due to streaming).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions