You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ Swiss Army Knife for SSE in Golang
5
5
So you want to publish events to client that connect to your server?
6
6
```go
7
7
funcmain() {
8
-
stream:=&eventsource.Stream{}
8
+
stream:= eventsource.NewStream()
9
9
10
10
gofunc(s *eventsource.Stream) {
11
11
for {
@@ -24,7 +24,7 @@ The `Stream` object implements an `http.Handler` for you so it can be registered
24
24
You got it! What do you need?
25
25
26
26
## Multiplexing / Topics / Rooms / Channels
27
-
We call them "topics" but the gist is the same. All Clients always recieve`Broadcast` events, but you can `Publish` events to a specific topic, and then only clients that have `Subscribe`d to that topic will recieve the event.
27
+
We call them "topics" but the gist is the same. All Clients always receive`Broadcast` events, but you can `Publish` events to a specific topic, and then only clients that have `Subscribe`d to that topic will receive the event.
You can also just create multiple `Stream` objects much to the same effect, then only use `Broadcast`. Streams are cheap and run no background routines, so this is a valid pattern.
Use the stream's `Register`, `Subscribe`, `Remove`, `Unsubscribe`, and `CloseTopic` functions to control which clients are registered where.
60
60
61
61
## Tell me when clients connect
62
-
Register a callback for the `Stream` to envoke everytime a new client connects with `Stream.ClientConnectHook`. It'll give you a handle to the resulting Client and the http request that created it, letting you do whatever you please.
62
+
Register a callback for the `Stream` to invoke every time a new client connects with `Stream.ClientConnectHook`. It'll give you a handle to the resulting Client and the http request that created it, letting you do whatever you please.
63
63
64
64
```go
65
-
stream:=&eventsource.Stream{}
65
+
stream:= eventsource.NewStream()
66
66
stream.ClientConnectHook(func(r *http.Requset, c *eventsource.Client){
67
67
fmt.Println("Recieved connection from", r.Host)
68
68
fmt.Println("Hate that guy")
@@ -71,13 +71,13 @@ stream.ClientConnectHook(func(r *http.Requset, c *eventsource.Client){
71
71
})
72
72
```
73
73
74
-
The callback will be on the same goroutine as the incoming web request that created it, but the Client is live and functioning so it'll start recieving broadcasts and publishments immediately before your callback has returned.
74
+
The callback will be on the same goroutine as the incoming web request that created it, but the Client is live and functioning so it'll start receiving broadcasts and publications immediately before your callback has returned.
75
75
76
76
## Graceful shutdown
77
-
The stream's `Shutdown` command will unsubscribe and disconnect all connected clients. However the `Stream` itself is not running any background routines, and may continue to register new clients if it's still registed as an http handler.
77
+
The stream's `Shutdown` command will unsubscribe and disconnect all connected clients. However the `Stream` itself is not running any background routines, and may continue to register new clients if it's still registered as an http handler.
78
78
79
79
## Get out of my way
80
-
Fine! The `Stream` object is entirely convinience. It runs no background routines and does no special handling. It just adds the topics abstraction and calls `NewClient` for you when it's connected to. Feel free not to use it.
80
+
Fine! The `Stream` object is entirely convenience. It runs no background routines and does no special handling. It just adds the topics abstraction and calls `NewClient` for you when it's connected to. Feel free not to use it.
81
81
82
82
# More control of the `Client`
83
83
You betcha.
@@ -101,12 +101,12 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request) {
101
101
Letting the http handler routine that created the Client return may cause the underlying connection to be closed by the server. Since `NewClient` does not block, use `Wait` to block until the client is shutdown.
102
102
103
103
## Shutdown the client
104
-
The client's `Shutdown` function terminates the background routine and marks the client as closed. It does not actually sever the connection. It does unblock any routines waiting on `Wait`, which assuming the main http hander routine was waiting there, will cause the connection to close as it returns.
104
+
The client's `Shutdown` function terminates the background routine and marks the client as closed. It does not actually sever the connection. It does unblock any routines waiting on `Wait`, which assuming the main http handler routine was waiting there, will cause the connection to close as it returns.
105
105
106
106
Attempts to `Send` events to a client after it has been shutdown will result in an error
107
107
108
108
# More control of Events
109
-
Events are the most critical part of the library, and are the most versitile.
109
+
Events are the most critical part of the library, and are the most versatile.
110
110
111
111
## Write my own events from scratch
112
112
Events implement the `io.ReadWriter` interface so that data can be written to it from practically any source. However the `Write` interface _does not_ write an entire event in wire format. It writes the provided buffer into `data:` sections in the resulting event.
@@ -135,7 +135,7 @@ newEvent.WriteRaw(evData) // that will work
135
135
newEvent.AppendData("Moar") // ... and you ruined it
136
136
```
137
137
138
-
Use `Clone` to create a perfect deep copy that survives further mutation. Though this is less efficient in memeory.
138
+
Use `Clone` to create a perfect deep copy that survives further mutation. Though this is less efficient in memory.
139
139
140
140
## Create events more easily
141
141
Since you will probably be creating more than just a few events, the `EventFactory` interface and a couple helper factories and functions have been provided to speed things up.
0 commit comments