Flux and AutobahnJs

I am using flux with authobahn and I have problems with my architecture. I have some components that get their status by subscribing to the pubsub theme.

I am currently using flux for my data, and my actions are as follows:

// I have a global session
window.AutobahnSession = ..;

// RoomsActions
var RoomsActions = {
  subscribeToRoom: function(roomName) {

    var onData = function(data) {
      Dispatcher.dispatch({
        type: "ROOM_MESSAGE_ARRIVED",
        message: data,
        roomName: roomName
      });
    };

    var subscription = window.AutobahnSession.subscribe('rooms/' + roomName, onData);

    // Sending the subscription to be persisted on the store,
    // So I can close & remove it later.
    Dispatcher.dispatch({
      type: "ROOM_SUBSCRIPTION_CREATED",
      subscription: subscription,
      roomName: roomName
    });

  },

  // Called on componentWillUnmount 
  unsubscribeToRoom: function(roomName) {
    Dispatcher.dispatch({
      type: "UNSUBSCRIBE_TO_ROOM",
      roomName: roomName
    });
  }

};

// RoomsStore
RoomsStore.dispatchToken = ChatAppDispatcher.register(function(action) {

  switch(action.type) {
    case "ROOM_MESSAGE_ARRIVED":
      // I have the message to a messages array and emitChange
      break;

    case "ROOM_SUBSCRIPTION_CREATED":
      // I add the subscription to mapping from roomName to subscription
      break;

    case "UNSUBSCRIBE_TO_ROOM":
      // Get the subscription by it room name, and close it and
      // remove from the mapping.
      break;
  }


});

Is this the right way to handle your subscription? I don’t really like the idea of ​​a subscribeToRoom action, which always listens for events and sends a subscription to the dispatcher.

+4
source share

Source: https://habr.com/ru/post/1610288/


All Articles