Vadim
Vadim

Reputation: 170

Websockets private messages/rooms

I want to make chat in my website ,but to use websockets I have to upgrade to VDS server(I have to pay more for that). Shortly could you help me whether my idea would work, i want to implement private chat for only two users, for example I have 4 users, websocket for user 1 and 2 will be wss://mywebsite.com:8080/chat.php?id=1 And for user 3 and 4 url will be wss://mywebsite.com:8080/chat.php?id=2 . So basically if I send smth to chat with id 1, chat with id 2 won't recieve that message as I want? Or different GET params will be treated as one websocket, not two?

Upvotes: 1

Views: 1834

Answers (2)

BGPHiJACK
BGPHiJACK

Reputation: 1397

You will probably want to have a data-broker that talks to the HTTP/S server and lets it know endpoints.

To elaborate!

Your chat-server would have a WSS Server, and a broker client (to send incoming data out) The data-broker will receive all connections/messages and route them back and forth, so say

User#1 on WSS1.yoursite.com:4020 sent a message to User#10109 at WSS209.yoursite.com:2945, the broker will middle-man and direct to whom it concerns whether it's a global or private message.

How you differentiate the users is through a session carried via the socket. You'd set a unique handle (identifier) that counts up indefinitely and so there could never be duplicate users or someone sniffing another persons message.

Setup is a bit savage, but if you want simple just run a unique handle per connection and map the socket so anytime you need to; you call the map if a match exists, send the message.

Upvotes: 1

Andy Nguyen
Andy Nguyen

Reputation: 339

With one pair chat between 1 and 2, your server has to open 2 sockets. 1 socket for chat between 1 and the server, the other socket for chat between 2 and the server.

As a beginning of one user connect to the server, you have to create a websocket for that user (if you want all user always ready for chatting). And now you have a set of websocket according to the id of users. Whenever some one send a chat over a socket along with an receiver's id, you now find out which socket to send to, with that receiver, and send the chat to receiver's socket. In most cases, you may want to send the chat back to the sender too.

Edit: dont worry about private chats, as a websocket created between one user and a server, a message from server to that socket can only be sent to the socket's user

Upvotes: 1

Related Questions