0:00 Demo 0:54 Overview of the hub architecture 2:25 Start working on createRoom 2:36 Define the hub, room, client, and the message structs + the hub constructor 5:30 Create the handler for/ws/createRoom endpoint 8:28 Call the hub and the handler in main.go and add the handler to the router 9:56 Start working on joinRoom 10:22 Instantiate the websocket connection in the joinRoom handler 15:35 Create the hub.Run() method and define the register, unregister, and broadcast channels 21:07 Call hub.Run() in main.go in a separate goroutine 21:29 Define the writeMessage() and readMessage() methods for the clients 26:17 Add the joinRoom handler to the /ws/joinRoom endpoint 26:50 Test the websocket connection on Postman 28:20 Create the /ws/getRooms endpoint and getRooms handler 30:15 Create the getClients handler and add the /ws/getClients endpoint 33:11 Test the endpoints
Great tutorial! I've got a question, though... Why do you take the client id and the username from a query param instead of the JWT you always get from the cookies? Re: 13:01 By the way I'm in love with your channel, I'm watching it alllll!
Haha I appreciate it!! Well that could technically work too since I stored the user id and username as part of the cookie. But I am just leaving it up to the frontend to handle the user id & username. I would use the JWT to authenticate the user across pages on the frontend and not to be directly used on the server side.
Is it ok for the CreateRoom handler to directly write to the Room map on the hub? Isn't there a concern about various routines writing to the same memory? Wasn't sure if I should create another channel for the hub to listen on for that.
what is the alternative of web sockets in production as they are not scalable in the product(monolith) and in serverless it is very complicated? Please reply
You could do Server-Sent Events (SSE). SSE is simpler and more scalable in serverless environments, as it maintains a single unidirectional connection from the server to the client for streaming updates. But if you need it to be bi-directional theres no other alternative that I know of than websockets.