S
S
Sergey Nikolaev2015-09-10 20:42:12
JavaScript
Sergey Nikolaev, 2015-09-10 20:42:12

What is the correct way to resubscribe to a socket to protect against breaks?

I use the socketIo library .
This is how I connect to the socket and listen:

socketIo.emit('subscribe', 'message');
socketIo.on('message', function (data) {
//
});

In the event of a disconnection, after reconnecting, subscriptions no longer occur. Of
course, you can cure it like this:
socketIo.on('connect', function (data) {
     socketIo.emit('subscribe', 'message');
});
socketIo.emit('subscribe', 'message');
socketIo.on('message', function (data) {
//
});

But it's kind of a crutch. Everywhere you have to prescribe the same thing twice.
How can I make sure that all subscriptions are automatically renewed at the moment of connection?
Please tell me a good implementation

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kozhin, 2015-09-17
@Devastor

Make a wrapper and add your "crutch". SocketIO (like sockjs) does not offer such a solution out of the box, so everyone adds on as needed.

I
Ivan Zmerzlyi, 2015-09-17
@DangelZM

Maybe so?

socketIo.on('disconnect', function () {
     socketIo.on('connect', function (data) {
         socketIo.emit('subscribe', 'message');
     });
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question