import { takeLatest, put } from 'redux-saga/effects';
import { eventChannel } from 'redux-saga';
import { actionsIDs } from '../constants'
import { fetchPatients } from './fetchPatients';
import { createStartHandler } from 'redux-saga-subscriptions';
import socket from '../../../socketIo/socket';

const msgChannel = 'patients-modified';
const getFetchEmit = (emit) => () => Promise.resolve(fetchPatients()).then(emit);

const createChannel = (payload?) => eventChannel((emit) => {
  const fetch = getFetchEmit(emit);

  fetch();

  socket.on(msgChannel, fetch);
  socket.on('disconnect', () => emit(put({type: 'failing-socket'})));
  socket.on('connect', () => emit(put({type: 'socket-alive'})));

  return () => {
    socket.off(msgChannel);
  };
});

export const watchPatientsSubscription = function *() {
  const startHandler = createStartHandler([actionsIDs.STOP_SUBSCRIBE]);
  yield takeLatest(actionsIDs.START_SUBSCRIBE, startHandler(createChannel));
};

import { takeLatest } from 'redux-saga/effects';
import { eventChannel } from 'redux-saga';
import { actionsIDs } from '../constants'
import { fetchPatients } from './fetchPatients';
import { createStartHandler } from 'redux-saga-subscriptions';
import socket from '../../../socketIo/socket';

const msgChannel = 'patients-modified';
const getFetchEmit = (emit) => () => Promise.resolve(fetchPatients()).then(emit);

const createChannel = (payload?) => eventChannel((emit) => {
  const fetch = getFetchEmit(emit);

  fetch();

  socket.on(msgChannel, fetch);

  return () => {
    socket.off(msgChannel);
  };
});

export const watchPatientsSubscription = function *() {
  const startHandler = createStartHandler([actionsIDs.STOP_SUBSCRIBE]);
  yield takeLatest(actionsIDs.START_SUBSCRIBE, startHandler(createChannel));
};

--- 
+++ 
@@ -1,4 +1,4 @@
-import { takeLatest } from 'redux-saga/effects';
+import { takeLatest, put } from 'redux-saga/effects';
 import { eventChannel } from 'redux-saga';
 import { actionsIDs } from '../constants'
 import { fetchPatients } from './fetchPatients';
@@ -14,6 +14,8 @@
   fetch();
 
   socket.on(msgChannel, fetch);
+  socket.on('disconnect', () => emit(put({type: 'failing-socket'})));
+  socket.on('connect', () => emit(put({type: 'socket-alive'})));
 
   return () => {
     socket.off(msgChannel);
Support for socket life-time events.
