title: React redux - how to handle events?
body: <p>I am learning the concepts of Redux.
  From what I understand, reducers are not supposed to mutate state, they return a new state with mutations applied. They are also not supposed to (or maybe even can't?) dispatch other actions.</p>
  <p>Given those limitations, that pretty much leaves you with the action creators, middleware, or your React components (if using Redux with React) to call your business logic. So, what I'm trying to do is use Firebase, the wall I've hit, and the questions I have are the following:</p>
  <ul>
  <li>Where should you be creating an instance (i.e. initialising Firebase)?
  Similarly, where should be uninitialising Firebase? Usually I would expect these to be in a service of some kind that you'd call, but I'm not really sure where you would make the call to that service.</li>
  <li>Where should you listen for changes? Firebase provides <strong>.on</strong> on references to allow you to react to changes (i.e. child_added, child_removed) - where should these .on handlers go? I was thinking that surely when one of these events comes in, an action needs to be dispatched with the child in question to update the state, but I can't do this in the reducers, I don't want to do this on a component, and the action creator just seems like an odd place for it to (and when I think about it, how would that even work?)!</li>
  </ul>
  <h2>Solution</h2>
  <p>Finally I ended up following the @ampersand suggestion. I made a module with all the listeners of Firebase application. I import in the Firebase module a <code>dispatcher.js</code> file, that is composed by dispatcher function like these:</p>
  <pre><code>import store from '../store';
  import * as actions from './index';
  
  export function addUserMessage(text) {
      store.dispatch(actions.addUserMessage(text));
  }
  
  export function addResponseMessage(messageObj) {
      store.dispatch(actions.addResponseMessage(messageObj));
  }
  </code></pre>
  <p>What I was not understanding was how to dispatch action from an external module. Now I have the concepts clear.</p>