@@ -77,28 +77,31 @@ import (
77
77
" context"
78
78
" fmt"
79
79
80
- " github.com/nitrictech/go-sdk/handler"
81
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
@@ -183,77 +179,74 @@ ws.On(handler.WebsocketMessage, func(ctx *handler.WebsocketContext, next handler
183
179
package main
184
180
185
181
import (
186
- " context"
187
- " fmt"
182
+ " context"
183
+ " fmt"
188
184
189
- " github.com/nitrictech/go-sdk/handler"
190
- " github.com/nitrictech/go-sdk/nitric"
185
+ " github.com/nitrictech/go-sdk/nitric"
186
+ " github.com/nitrictech/go-sdk/nitric/keyvalue"
187
+ " github.com/nitrictech/go-sdk/nitric/websockets"
191
188
)
192
189
193
190
func main () {
194
- ws , err := nitric.NewWebsocket (" public" )
195
- if err != nil {
196
- fmt.Println (" Error creating WebSocket:" , err)
197
- return
198
- }
199
-
200
- connections , err := nitric.NewKv (" connections" ).Allow (nitric.KvStoreGet , nitric.KvStoreSet , nitric.KvStoreDelete )
201
- if err != nil {
202
- fmt.Println (" Error creating KV store:" , err)
203
- return
204
- }
205
-
206
- ws.On (handler.WebsocketConnect , func (ctx *handler.WebsocketContext , next handler.WebsocketHandler ) (*handler.WebsocketContext , error ) {
207
- err := connections.Set (context.TODO (), ctx.Request .ConnectionID (), map [string ]interface {}{
208
- " connectionId" : ctx.Request .ConnectionID (),
209
- })
210
- if err != nil {
211
- return ctx, err
212
- }
213
-
214
- return next (ctx)
215
- })
216
-
217
- ws.On (handler.WebsocketDisconnect , func (ctx *handler.WebsocketContext , next handler.WebsocketHandler ) (*handler.WebsocketContext , error ) {
218
- err := connections.Delete (context.TODO (), ctx.Request .ConnectionID ())
219
- if err != nil {
220
- return ctx, err
221
- }
222
-
223
- return next (ctx)
224
- })
225
-
226
- ws.On (handler.WebsocketMessage , func (ctx *handler.WebsocketContext , next handler.WebsocketHandler ) (*handler.WebsocketContext , error ) {
227
- connectionStream , err := connections.Keys (context.TODO ())
228
- if err != nil {
229
- return ctx, err
230
- }
231
-
232
- senderId := ctx.Request .ConnectionID ()
233
-
234
- for {
235
- connectionId , err := connectionStream.Recv ()
236
- if err != nil {
237
- break
238
- }
239
-
240
- if connectionId == senderId {
241
- continue
242
- }
243
-
244
- message := fmt.Sprintf (" %s : %s " , senderId, ctx.Request .Message ())
245
- err = ws.Send (context.TODO (), connectionId, []byte (message))
246
- if err != nil {
247
- return ctx, err
248
- }
249
- }
250
-
251
- return next (ctx)
252
- })
253
-
254
- if err := nitric.Run (); err != nil {
255
- fmt.Println (" Error running Nitric service:" , err)
256
- }
191
+ // Create a WebSocket endpoint named "public".
192
+ ws := nitric.NewWebsocket (" public" )
193
+
194
+ // Initialize a KV store named "connections" with Get, Set, and Delete permissions.
195
+ connections := nitric.NewKv (" connections" ).Allow (keyvalue.KvStoreGet , keyvalue.KvStoreSet , keyvalue.KvStoreDelete )
196
+
197
+ // Handle new WebSocket connections by storing the connection ID in the KV store.
198
+ ws.On (websockets.EventType_Connect , func (ctx *websockets.Ctx ) {
199
+ err := connections.Set (context.TODO (), ctx.Request .ConnectionID (), map [string ]interface {}{
200
+ " connectionId" : ctx.Request .ConnectionID (),
201
+ })
202
+ if err != nil {
203
+ return
204
+ }
205
+ })
206
+
207
+ ws.On (websockets.EventType_Connect , func (ctx *websockets.Ctx ) {
208
+ err := connections.Set (context.TODO (), ctx.Request .ConnectionID (), map [string ]interface {}{
209
+ " connectionId" : ctx.Request .ConnectionID (),
210
+ })
211
+ if err != nil {
212
+ return
213
+ }
214
+ })
215
+
216
+ ws.On (websockets.EventType_Disconnect , func (ctx *websockets.Ctx ) {
217
+ err := connections.Delete (context.TODO (), ctx.Request .ConnectionID ())
218
+ if err != nil {
219
+ return
220
+ }
221
+ })
222
+
223
+ ws.On (websockets.EventType_Message , func (ctx *websockets.Ctx ) {
224
+ connectionStream , err := connections.Keys (context.TODO ())
225
+ if err != nil {
226
+ return
227
+ }
228
+
229
+ senderId := ctx.Request .ConnectionID ()
230
+
231
+ for {
232
+ connectionId , err := connectionStream.Recv ()
233
+ if err != nil {
234
+ break
235
+ }
236
+
237
+ if connectionId == senderId {
238
+ continue
239
+ }
240
+
241
+ message := fmt.Sprintf (" %s : %s " , senderId, ctx.Request .Message ())
242
+ err = ws.Send (context.TODO (), connectionId, []byte (message))
243
+ if err != nil {
244
+ return
245
+ }
246
+ }
247
+ })
248
+
249
+ nitric.Run ()
257
250
}
258
251
```
259
252
0 commit comments