Conversation started Mar 23, 2016 at 20:38.
Mar 23, 2016 20:38
@DanPantry Couldn't the API just subscribe to the store?
> @Zomis does the game server broadcast a message to all connected clients when a game invite is sent between 2 players?
> Pretty certain I fixed this a good while back and forgot to close it.
constructor(store) {
    ... initilaize websocket
    store.subscribe = sendMessage
}
sendMessage(action) {
    if(action.type === SEND_MESSAGE) {
        ...
    }
}
for example.
Mar 23, 2016 20:41
Sorry I'll stop spamming Duga for a bit
Mar 23, 2016 20:57
@SirPython that seems like a bad idea
plus that would only cover sending messages ,not receiving
I'm only referring to requests to send messages right now. Isn't that you were you referring to, also, when you gave the middleware example?
Middleware doesn't modify store.subscribe. It just sits between action creators and reducers
you apply it to the store itself
For sending a message, yes, you'd probably just need one action creator as long as all data has the same schema.
function sendMessage(id, payload) {
  return {
    type: SEND_MESSAGE,
    payload,
    id
  }
}
....never mind I am an idiot :-)
Messages should either way be sent in action creators, so... maybe something like this
function sendMessage(id, payload) {
  return dispatch => {
    websocket.emit({ id, payload })
    dispatch(sentMessage())
  }
}
alternatively you could, again, send messages in middleware
So, that would look like this:
    function sendMessage(id, payload) {
      return {
        type: SEND_MESSAGE,
        id,
        payload
      }
    }

    const websocketMiddleware = store => next => action => {
      switch (action.type) {
        case SEND_MESSAGE:
          socket.emit(...)
          return next(action);
        case RECEIVE_MESSAGE:
          // read the message.. somehow...
          return next(action);
      }
    }

   const store = applyMiddleware(websocketMiddleware)(createStore(reducer))
Oh shoot, I didn't mean to say store.subscribe = ... I mean to say store.subscribe(this.sendMessage)
No, that's still not ideal, because store.subscribe means you are notified of every internal action (potentially)
Though, I guess just a normal reducer is the same
Still, I think the middleware approach would be better
you could unmarshal the message in RECIEVE_MESSAGE and just return a completely different action (with a different type)
Mar 23, 2016 21:13
Okay. I still need to learn about middleware though.
this would enable the game to be driven by normal action creators with the web socket removed
 
Conversation ended Mar 23, 2016 at 21:13.