@@ -74,31 +74,34 @@ Let's begin by setting up the WebSocket application. First, create a new folder
74
74
package main
75
75
76
76
import (
77
- " context"
78
- " fmt"
77
+ " context"
78
+ " fmt"
79
79
80
- " github.com/nitrictech/go-sdk/handler"
81
- " github.com/nitrictech/go-sdk/nitric"
80
+ " github.com/nitrictech/go-sdk/nitric"
81
+ " github.com/nitrictech/go-sdk/nitric/keyvalue"
82
+ " github.com/nitrictech/go-sdk/nitric/websockets"
82
83
)
83
84
84
85
func main () {
85
- ws , err := nitric.NewWebsocket (" public" )
86
- if err != nil {
87
- fmt.Println (" Error creating WebSocket:" , err)
88
- return
89
- }
86
+ // Create a WebSocket endpoint named "public".
87
+ ws := nitric.NewWebsocket (" public" )
90
88
91
- connections , err := nitric.NewKv (" connections" ).Allow (nitric.KvStoreGet , nitric.KvStoreSet , nitric.KvStoreDelete )
92
- if err != nil {
93
- fmt.Println (" Error creating KV store:" , err)
94
- return
95
- }
89
+ // Initialize a KV store named "connections" with Get, Set, and Delete permissions.
90
+ connections := nitric.NewKv (" connections" ).Allow (keyvalue.KvStoreGet , keyvalue.KvStoreSet , keyvalue.KvStoreDelete )
91
+
92
+ // Handle new WebSocket connections by storing the connection ID in the KV store.
93
+ ws.On (websockets.EventType_Connect , func (ctx *websockets.Ctx ) {
94
+ err := connections.Set (context.TODO (), ctx.Request .ConnectionID (), map [string ]interface {}{
95
+ " connectionId" : ctx.Request .ConnectionID (),
96
+ })
97
+ if err != nil {
98
+ return
99
+ }
100
+ })
96
101
97
102
// Add event handlers here
98
103
99
- if err := nitric.Run (); err != nil {
100
- fmt.Println (" Error running Nitric service:" , err)
101
- }
104
+ nitric.Run ()
102
105
}
103
106
```
104
107
@@ -117,40 +120,35 @@ From here, let's add some features to that function that allow us to manage conn
117
120
### Register connections on connect
118
121
119
122
``` go
120
- ws.On (handler. WebsocketConnect , func (ctx *handler. WebsocketContext , next handler. WebsocketHandler ) (*handler. WebsocketContext , error ) {
123
+ ws.On (websockets. EventType_Connect , func (ctx *websockets. Ctx ) {
121
124
err := connections.Set (context.TODO (), ctx.Request .ConnectionID (), map [string ]interface {}{
122
125
" connectionId" : ctx.Request .ConnectionID (),
123
126
})
124
127
if err != nil {
125
- return ctx, err
128
+ return
126
129
}
127
-
128
- return next (ctx)
129
130
})
130
131
```
131
132
132
133
### Remove connections on disconnect
133
134
134
135
``` go
135
- ws.On (handler. WebsocketDisconnect , func (ctx *handler. WebsocketContext , next handler. WebsocketHandler ) (*handler. WebsocketContext , error ) {
136
+ ws.On (websockets. EventType_Disconnect , func (ctx *websockets. Ctx ) {
136
137
err := connections.Delete (context.TODO (), ctx.Request .ConnectionID ())
137
138
if err != nil {
138
- return ctx, err
139
+ return
139
140
}
140
-
141
- return next (ctx)
142
141
})
143
142
```
144
143
145
144
### Broadcast messages to all connected clients
146
145
147
146
``` go
148
- ws.On (handler. WebsocketMessage , func (ctx *handler. WebsocketContext , next handler. WebsocketHandler ) (*handler. WebsocketContext , error ) {
147
+ ws.On (websockets. EventType_Message , func (ctx *websockets. Ctx ) {
149
148
connectionStream , err := connections.Keys (context.TODO ())
150
149
if err != nil {
151
- return ctx, err
150
+ return
152
151
}
153
-
154
152
senderId := ctx.Request .ConnectionID ()
155
153
156
154
for {
@@ -166,11 +164,9 @@ ws.On(handler.WebsocketMessage, func(ctx *handler.WebsocketContext, next handler
166
164
message := fmt.Sprintf (" %s : %s " , senderId, ctx.Request .Message ())
167
165
err = ws.Send (context.TODO (), connectionId, []byte (message))
168
166
if err != nil {
169
- return ctx, err
167
+ return
170
168
}
171
169
}
172
-
173
- return next (ctx)
174
170
})
175
171
```
176
172
@@ -186,8 +182,8 @@ import (
186
182
" context"
187
183
" fmt"
188
184
189
- " github.com/nitrictech/go-sdk/handler"
190
185
" github.com/nitrictech/go-sdk/nitric"
186
+ " github.com/nitrictech/go-sdk/nitric/websockets"
191
187
)
192
188
193
189
func main () {
@@ -203,30 +199,26 @@ func main() {
203
199
return
204
200
}
205
201
206
- ws.On (handler. WebsocketConnect , func (ctx *handler. WebsocketContext , next handler. WebsocketHandler ) (*handler. WebsocketContext , error ) {
202
+ ws.On (websockets. EventType_Connect , func (ctx *websockets. Ctx ) {
207
203
err := connections.Set (context.TODO (), ctx.Request .ConnectionID (), map [string ]interface {}{
208
204
" connectionId" : ctx.Request .ConnectionID (),
209
205
})
210
206
if err != nil {
211
- return ctx, err
207
+ return
212
208
}
213
-
214
- return next (ctx)
215
209
})
216
210
217
- ws.On (handler. WebsocketDisconnect , func (ctx *handler. WebsocketContext , next handler. WebsocketHandler ) (*handler. WebsocketContext , error ) {
211
+ ws.On (websockets. EventType_Disconnect , func (ctx *websockets. Ctx ) {
218
212
err := connections.Delete (context.TODO (), ctx.Request .ConnectionID ())
219
213
if err != nil {
220
- return ctx, err
214
+ return
221
215
}
222
-
223
- return next (ctx)
224
216
})
225
217
226
- ws.On (handler. WebsocketMessage , func (ctx *handler. WebsocketContext , next handler. WebsocketHandler ) (*handler. WebsocketContext , error ) {
218
+ ws.On (websockets. EventType_Message , func (ctx *websockets. Ctx ) {
227
219
connectionStream , err := connections.Keys (context.TODO ())
228
220
if err != nil {
229
- return ctx, err
221
+ return
230
222
}
231
223
232
224
senderId := ctx.Request .ConnectionID ()
@@ -244,16 +236,12 @@ func main() {
244
236
message := fmt.Sprintf (" %s : %s " , senderId, ctx.Request .Message ())
245
237
err = ws.Send (context.TODO (), connectionId, []byte (message))
246
238
if err != nil {
247
- return ctx, err
239
+ return
248
240
}
249
241
}
250
-
251
- return next (ctx)
252
242
})
253
243
254
- if err := nitric.Run (); err != nil {
255
- fmt.Println (" Error running Nitric service:" , err)
256
- }
244
+ nitric.Run ()
257
245
}
258
246
```
259
247
0 commit comments