Ankit
Ankit

Reputation: 121

How to make live interaction between users with browsers

I have a web page with four textarea. Suppose page URL is example.com?Id=5

Now what I want to achieve is when two users visit this page. And one of them make change in the content of any text area. Then it should reflect the changes in the window of all other users of same URL. Please give instructions to achieve this.

Upvotes: 0

Views: 93

Answers (1)

rbtLong
rbtLong

Reputation: 1572

Websockets can be a great solution. For instance, you can create a pub/sub design pattern where the two users will subscribe to the server via a websocket for instance . . .

var ws = new WebSocket('ws://yourdomain.com/ws-route');
ws.onmessage = function(m) {
console.log(m); // this is how you will receive your messages
ws.send('i am acknowledging your message and sending you back mine.');
}

every send from either users will be read by the server and published back to both users (or the other user depending on your strategy). in this way, you can achieve this effect. for instance, you can hook the on keyup or something on the textarea to ws.send using the value of the textarea. then you can hook the onmessage even to change the value of textarea whenever you receive a new messsage.

Upvotes: 1

Related Questions