I m develloping a web application, in which I have to show, let's say, a list of tasks.
What I need to do is once a user open a task (i.e open it in his browser), it gets deleted of all the others users lists or it shows that the task is taken, no one can open a task that is already opened by someone else.
In addition, I need that once a user close a task (for example user closes his browser) , it get re-added to other users tasks list.
How can I do that please ? WebSocket in client side ? Any other -better- choices?
How to manage this in the server side ?
Thank you

- 35,300
- 7
- 55
- 79

- 1,209
- 1
- 15
- 33
-
1What have you done till this point to achieve this behavior? What is the technology stack you are using? (eg. JQuery on front end, Spring for middleware, etc.). You could fire an ajax query on 'click' of the task, and let the server handle the removal of that task from the 'pool of tasks', and fire another ajax query when the user 'closes' the task. At this point, this is all I can tell you. – Chetan Jadhav CD May 31 '16 at 20:00
-
Didn't start coding until I get a clear idea about to "doability" of this, I thought about opening a websocket in the client side, with a while(true) to keep the lock on the task, but not sure about that, I've never done this kind of things before (use websocket) have only read some docs, but I think with -for example- 100.000 websocket connecting my server in the same time checking the locks won't be a good idea... – hereForLearing May 31 '16 at 20:03
-
have just seen your edit, you can say that I'm using Spring (actually I'm using grails, but it's the same thing, grails have less users here so prefered to make it a java question), I've read an article about using ajax to do that, it's not recommended, since the answers from the server may return in disorder – hereForLearing May 31 '16 at 20:06
-
1If you want the "Opening of the task" activity by one user to have an effect immediately on all the other users, then hacks like ajax based-polling don't seem like a good idea. Websockets would be the preferred approach. Have a look at the answer to this post [link](http://stackoverflow.com/questions/31035467/push-notification-is-websocket-mandatory) – Chetan Jadhav CD May 31 '16 at 20:07
-
1Btw, I don't know why your question is being downvoted. Seems like a legit question to me. – Chetan Jadhav CD May 31 '16 at 20:13
-
Thank you sir, a stupid question, if each user open for example 5 pages, and there is a 100k users connected, will I have 500k sockets opened to my server? won't this "hurt" my server? – hereForLearing May 31 '16 at 20:18
-
me too... downvote lovers... – hereForLearing May 31 '16 at 20:18
-
1Depends on how you implement your final solution. Since a websocket gets created per page and if all of them are "alive" in different tabs, across 100s of users, you could potentially run into a scenario where you end up creating a lot of websocket connections. However, you could use something like [Web Workers](http://tavendo.com/blog/post/websocket-persistent-connections/). Or a framework to manage websockets. I do not have much knowledge about those so you could research on your own on that, and see how you can effectively manage websocket connections. Good luck! – Chetan Jadhav CD May 31 '16 at 20:31
1 Answers
There is two solutions (that I know of) for your problem :
- Ajax (and Long Poll)
Using Ajax you can poll the server every X seconds for data necessary to implement the behavior you described. This may work but it is not a recommended way to go. read why on this thread.
- WebSocket
Just like a socket you'd use on a desktop application (hence the name), you can open a WebSocket connection (not an HTTP one), keep it as long as needed and close it only when you're done with it. This is the preferred way to have data flowing constantly between a client and a server. It is also supported by almost every major browser out there
There is other technologies such as WebRTC and Server-Sent Events, but considering your description of the problem, those might not be what you're looking for.
Off-topic : And I'm not sure why your question has been downvoted, looks legit to me.