-
Notifications
You must be signed in to change notification settings - Fork 1.8k
added built in SSE support in GoFr #3044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
IronwallxR5
wants to merge
10
commits into
gofr-dev:development
Choose a base branch
from
IronwallxR5:build-in-sse-suport
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+828
−4
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1475fe2
added buid in SSE support in GoFr
IronwallxR5 2bab9f9
fix: resolve golangci-lint violations in SSE implementation
IronwallxR5 8b26c84
fixed code quality
IronwallxR5 b0dfd86
switch SSE to sequential Responder callback
IronwallxR5 1762962
fix golangci-lint errors in sse tests
IronwallxR5 860acff
Merge branch 'development' into build-in-sse-suport
Umang01-hash d964993
address review feedback on sse implementation
IronwallxR5 9089301
fixed typo in responder.go
IronwallxR5 76b46bf
Merge branch 'development' into build-in-sse-suport
IronwallxR5 597ad89
Merge branch 'development' into build-in-sse-suport
IronwallxR5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| APP_NAME=using-sse | ||
| HTTP_PORT=9000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "gofr.dev/pkg/gofr" | ||
| ) | ||
|
|
||
| func main() { | ||
| app := gofr.New() | ||
|
|
||
| // Stream the current time every second. | ||
| // c.Context.Done() fires on both client disconnect and server shutdown. | ||
| app.GET("/events", func(c *gofr.Context) (any, error) { | ||
| return gofr.SSEResponse(func(stream *gofr.SSEStream) error { | ||
| ticker := time.NewTicker(time.Second) | ||
| defer ticker.Stop() | ||
|
|
||
| i := 0 | ||
|
|
||
| for { | ||
| select { | ||
| case <-c.Context.Done(): | ||
| // Graceful cleanup: release resources, close DB cursors, etc. | ||
| return nil | ||
| case t := <-ticker.C: | ||
| if err := stream.Send(gofr.SSEEvent{ | ||
| ID: fmt.Sprintf("%d", i), | ||
| Name: "time", | ||
| Data: map[string]string{"time": t.Format(time.RFC3339)}, | ||
| }); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| i++ | ||
| } | ||
| } | ||
| }), nil | ||
| }) | ||
|
|
||
| // A countdown that sends 11 events and closes. | ||
| app.GET("/countdown", func(c *gofr.Context) (any, error) { | ||
| return gofr.SSEResponse(func(stream *gofr.SSEStream) error { | ||
| ticker := time.NewTicker(500 * time.Millisecond) | ||
| defer ticker.Stop() | ||
|
|
||
| for i := 10; i >= 0; i-- { | ||
| select { | ||
| case <-c.Context.Done(): | ||
| return nil | ||
| case <-ticker.C: | ||
| if err := stream.SendEvent("countdown", map[string]int{"remaining": i}); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return stream.SendEvent("done", "Countdown complete!") | ||
| }), nil | ||
| }) | ||
|
|
||
| app.Run() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package response | ||
|
|
||
| // SSE represents a Server-Sent Events response. | ||
| // Return this from a handler to stream events to the client. | ||
| type SSE struct { | ||
| // Callback holds the user's SSE streaming function. | ||
| // Typed as any to avoid circular imports; the Responder type-asserts it at call-site. | ||
| Callback any | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.