I have a socket.io application. Is there a way to temporarily lock a connection (Only a single connection)for a certain amount of time(say 30 seconds) without dropping the connection. What I want to do is, after an event is triggered in the client side, I want that event not to be triggered again before 30 seconds. By the way I am unable to do this in the client side because of the project restrictions.
2 Answers
Firstly I don't know any restrictions on server-side but it might affect client-side also , however basically you should create a room and then you must specify to which room gets socket messages. You can create like activeRoom
and suspendedClients
(room namings are completely as an example).
Then you can specify room to send message. To create 30 sec timing for specific user; get his socketId
and set its countdown time 30
as a json object.
// Multiple Clients may be suspended , use case ?.
var suspendedClients = [{
socketId: 123981787,
countdown: 30
},{
socketId: 987987788,
countdown: 21
}];
// That users are also in suspendedClients room so they can't get messages.
// Also you might use redis to store them(?. if there is no restrictions about to use a new datastore).
You may check in setInterval
function per second suspended clients.
if(suspendedClients[i].countdown == 0) {
// Remove client from suspendedClients room.
}
This idea is a general concept. You can shape that idea according to your exact requirements.

- 3,129
- 3
- 30
- 51
Socket.io hasn't exactly that function. But you can code it in many ways
in any case you need to stop sending that event or stop receiving
If you can't modify client side, just save socket.id
in array of stopped
and setTimeout for 30 seconds to pull it out from array
another approach to subscribe / unsubscribe
listeners
How to unsubscribe from a socket.io subscription?
and I'm thinking you can use rooms for that logic.
- All sockets in one room and all listeners send / receive for that room
- After event fired, you leave that room for your socket
- After 30 seconds you join to it again