» Go: Make Web Chat App with Socket.IO » 2. Development » 2.2 Contacts

Contacts

Use a map[string]map[string]interface{} (map of maps) to store all the users in the memory.

You need to use databases for large-scale projects.

Update main.go:

@@ -8,6 +8,8 @@ import (
 	"github.com/zishang520/socket.io/v2/socket"
 )
 
+type User = map[string]interface{}
+
 const port = 4000
 
 func main() {
@@ -15,10 +17,32 @@ func main() {
 	opts := &socket.ServerOptions{}
 	opts.SetCors(&types.Cors{Origin: "*"})
 
+	users := make(map[string]User)
+
 	io := socket.NewServer(nil, opts)
 	io.On("connection", func(clients ...any) {
 		client := clients[0].(*socket.Socket)
 		fmt.Println("A user connected:", client.Id())
+
+		client.On("user-join", func(args ...any) {
+			user, ok := args[0].(User)
+			if !ok {
+				return
+			}
+			if _, ok := user["name"]; !ok {
+				return
+			}
+			id := string(client.Id())
+			fmt.Printf("User %s => %s %s joined\n", id, user["emoji"], user["name"])
+			user["sid"] = id
+			users[id] = user
+			// Broadcast to all connected clients
+			var items [][]interface{}
+			for k, v := range users {
+				items = append(items, []interface{}{k, v})
+			}
+			io.Sockets().Emit("contacts", items)
+		})
 	})
 
 	http.Handle("/socket.io/", io.ServeHandler(nil))
  • var items [][]interface{} is an array of arrays, which is easier for the js client to parse as a js Map.
  • io.Sockets().Emit will broadcast the latest contact list to all the users.
PrevNext