@@ -29,13 +29,13 @@ tags:
2929We'll start by creating a new project for our WebSocket application.
3030
3131``` bash 
32- nitric new my- websocket-app go-starter
32+ nitric new websocket-app go-starter
3333``` 
3434
3535Next, open the project in your editor of choice.
3636
3737``` bash 
38- cd  my- websocket-app
38+ cd  websocket-app
3939``` 
4040
4141Make sure all dependencies are resolved:
@@ -111,10 +111,11 @@ From here, let's add some features to that function that allow us to manage conn
111111
112112``` go 
113113ws.On (websockets.EventType_Connect , func (ctx *websockets.Ctx ) {
114-   err  :=  connections.Set (context.TODO (), ctx.Request .ConnectionID (), map [string ]interface {}{
114+   err  :=  connections.Set (context.Background (), ctx.Request .ConnectionID (), map [string ]interface {}{
115115    " connectionId"  : ctx.Request .ConnectionID (),
116116  })
117117  if  err != nil  {
118+     fmt.Println (" Error storing connection ID in KV store:"  , err)
118119    return 
119120  }
120121})
@@ -126,6 +127,7 @@ ws.On(websockets.EventType_Connect, func(ctx *websockets.Ctx) {
126127ws.On (websockets.EventType_Disconnect , func (ctx *websockets.Ctx ) {
127128  err  :=  connections.Delete (context.Background (), ctx.Request .ConnectionID ())
128129  if  err != nil  {
130+     fmt.Println (" Error deleting connection ID in KV store:"  , err)
129131    return 
130132  }
131133})
@@ -135,8 +137,9 @@ ws.On(websockets.EventType_Disconnect, func(ctx *websockets.Ctx) {
135137
136138``` go 
137139ws.On (websockets.EventType_Message , func (ctx *websockets.Ctx ) {
138-   connectionStream , err  :=  connections.Keys (context.TODO ())
140+   connectionStream , err  :=  connections.Keys (context.Background ())
139141  if  err != nil  {
142+     fmt.Println (" Error retrieving connection keys from KV store:"  , err)
140143    return 
141144  }
142145
@@ -155,6 +158,7 @@ ws.On(websockets.EventType_Message, func(ctx *websockets.Ctx) {
155158    message  :=  fmt.Sprintf (" %s : %s "  , senderId, ctx.Request .Message ())
156159    err = ws.Send (context.Background (), connectionId, []byte (message))
157160    if  err != nil  {
161+       fmt.Println (" Error sending message to connection ID"  , connectionId, " :"  , err)
158162      return 
159163    }
160164  }
@@ -187,24 +191,27 @@ func main() {
187191
188192  //  Handle new WebSocket connections by storing the connection ID in the KV store.
189193  ws.On (websockets.EventType_Connect , func (ctx *websockets.Ctx ) {
190-     err  :=  connections.Set (context.TODO (), ctx.Request .ConnectionID (), map [string ]interface {}{
194+     err  :=  connections.Set (context.Background (), ctx.Request .ConnectionID (), map [string ]interface {}{
191195      " connectionId"  : ctx.Request .ConnectionID (),
192196    })
193197    if  err != nil  {
198+       fmt.Println (" Error storing connection ID in KV store:"  , err)
194199      return 
195200    }
196201  })
197202
198203  ws.On (websockets.EventType_Disconnect , func (ctx *websockets.Ctx ) {
199-     err  :=  connections.Delete (context.TODO (), ctx.Request .ConnectionID ())
204+     err  :=  connections.Delete (context.Background (), ctx.Request .ConnectionID ())
200205    if  err != nil  {
206+       fmt.Println (" Error deleting connection ID in KV store:"  , err)
201207      return 
202208    }
203209  })
204210
205211  ws.On (websockets.EventType_Message , func (ctx *websockets.Ctx ) {
206-     connectionStream , err  :=  connections.Keys (context.TODO ())
212+     connectionStream , err  :=  connections.Keys (context.Background ())
207213    if  err != nil  {
214+       fmt.Println (" Error retrieving connection keys from KV store:"  , err)
208215      return 
209216    }
210217
@@ -221,8 +228,9 @@ func main() {
221228      }
222229
223230      message  :=  fmt.Sprintf (" %s : %s "  , senderId, ctx.Request .Message ())
224-       err = ws.Send (context.TODO (), connectionId, []byte (message))
231+       err = ws.Send (context.Background (), connectionId, []byte (message))
225232      if  err != nil  {
233+         fmt.Println (" Error sending message to connection ID"  , connectionId, " :"  , err)
226234        return 
227235      }
228236    }
0 commit comments