✅ SignalR / Websockets
hi, i need to create app where u have x amount of chats, when u connect to chat, u can write messages and other ppl form same chat need to see it live, and all this without using any front-end, can anyone set me on the right path with some advices?
4 Replies
use SignalR / Websockets
so, as i get, ws's are way to connect to server once and exchange data without multiple requests, and my goal is to create that type endpoint, where someone connects to server with this ws.
and communication between 2+ ppl is achived by, 2 or more ppl connected to this server with same ws at same time, and for example 1 sends message to server, other clients recive copy of it by server?
is it right way?
Websockets are more like a non-hacky form of long-polling, if you know what that is.
The client makes a normal HTTP request to the server, but flags it as a request for a WebSocket.
The server sees that request and flag, and decides whether to accept the request as a WebSocket connection. If so, it sends an initial response, indicating acceptance, but then holds-open the TCP connection for that request, allowing the client to write additional data to the request body, and the server to write additional data to the response body.
Generally, you'll use a protocol on top of the WebSockets framework to packetize additional writes and reads on the streams, into "messages".
On a more practical note, no, two clients will not connect to the same WebSocket, each will have its own WebSocket connection, on whatever HTTP endpoint you see fit to implement, and it's up to you to manage transfer of data from one connection to others, by whatever logic you want
SignalR is what you want to try out, as it has a built-in framework on top of WebSockets that it calls the "Hubs" API, which is exactly what you describe. Clients connect to Hubs, and theframework provides a relatively-simple Server-side API for receiving messages from clients on the hub, and sending messages out to all or some of the clients connected to the hub.
Yeah, hubs should be it. I was just a bit confused about what I needed to do because there were some weird instructions in the task (personally form me, cuz never worked with websockts, and that postman things just to connect and send message is kinda complicated, with js it looks much easier). From what I got from all the guides and articles ive read, creating groups by chat id should be easy. Chats from the database have nothing to do with these groups except for having the same name/Id.
Before I get this, I thought i needed to have pre-made groups and api endpoint which will connect user to right websocket based on request. Now, ill create connections based on requests, and I don't have to worry about who and how will find way how to make right request. I just need to split clients based on request data into groups and process their request, send message to users in group and save it to db. It feels easy to do, but some aspects of the task still feel a bit strange, especially because theres no front-end. I just need to understand that I don't have to care how users connect, i just need to handle it when it happens
🐗