Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit f59c5ed

Browse files
committed
fix: update code for new go SDK
1 parent 715e936 commit f59c5ed

File tree

1 file changed

+88
-95
lines changed

1 file changed

+88
-95
lines changed

src/pages/guides/go/realtime-messaging.mdx

Lines changed: 88 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -77,28 +77,31 @@ import (
7777
"context"
7878
"fmt"
7979

80-
"github.com/nitrictech/go-sdk/handler"
8180
"github.com/nitrictech/go-sdk/nitric"
81+
"github.com/nitrictech/go-sdk/nitric/keyvalue"
82+
"github.com/nitrictech/go-sdk/nitric/websockets"
8283
)
8384

8485
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")
9088

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+
})
96101

97102
// Add event handlers here
98103

99-
if err := nitric.Run(); err != nil {
100-
fmt.Println("Error running Nitric service:", err)
101-
}
104+
nitric.Run()
102105
}
103106
```
104107

@@ -117,40 +120,35 @@ From here, let's add some features to that function that allow us to manage conn
117120
### Register connections on connect
118121

119122
```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) {
121124
err := connections.Set(context.TODO(), ctx.Request.ConnectionID(), map[string]interface{}{
122125
"connectionId": ctx.Request.ConnectionID(),
123126
})
124127
if err != nil {
125-
return ctx, err
128+
return
126129
}
127-
128-
return next(ctx)
129130
})
130131
```
131132

132133
### Remove connections on disconnect
133134

134135
```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) {
136137
err := connections.Delete(context.TODO(), ctx.Request.ConnectionID())
137138
if err != nil {
138-
return ctx, err
139+
return
139140
}
140-
141-
return next(ctx)
142141
})
143142
```
144143

145144
### Broadcast messages to all connected clients
146145

147146
```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) {
149148
connectionStream, err := connections.Keys(context.TODO())
150149
if err != nil {
151-
return ctx, err
150+
return
152151
}
153-
154152
senderId := ctx.Request.ConnectionID()
155153

156154
for {
@@ -166,11 +164,9 @@ ws.On(handler.WebsocketMessage, func(ctx *handler.WebsocketContext, next handler
166164
message := fmt.Sprintf("%s: %s", senderId, ctx.Request.Message())
167165
err = ws.Send(context.TODO(), connectionId, []byte(message))
168166
if err != nil {
169-
return ctx, err
167+
return
170168
}
171169
}
172-
173-
return next(ctx)
174170
})
175171
```
176172

@@ -183,77 +179,74 @@ ws.On(handler.WebsocketMessage, func(ctx *handler.WebsocketContext, next handler
183179
package main
184180

185181
import (
186-
"context"
187-
"fmt"
182+
"context"
183+
"fmt"
188184

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"
191188
)
192189

193190
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()
257250
}
258251
```
259252

0 commit comments

Comments
 (0)