react-native-action-cable icon indicating copy to clipboard operation
react-native-action-cable copied to clipboard

App is connecting to action cable multiple times

Open muhsin-k opened this issue 5 years ago • 11 comments

ActionCableConnector.js


import { ActionCable, Cable } from '@kesha-antonov/react-native-action-cable';
import { WEB_SOCKET_URL } from '../constants/url';

const connectActionCable = ActionCable.createConsumer(WEB_SOCKET_URL);

const cable = new Cable({});

import { getPubSubToken } from './AuthHelper';

import {
  addConversation,
  addMessageToConversation,
} from '../actions/conversation';

import { store } from '../store';

class ActionCableConnector {
  constructor(pubSubToken) {
    const channel = cable.setChannel(
      'RoomChannel',
      connectActionCable.subscriptions.create({
        channel: 'RoomChannel',
        pubsub_token: pubSubToken,
      }),
    );

    channel.on('received', this.onReceived);

    this.events = {
      'messageCreated': this.onMessageCreated,
    };
  }

  onReceived = ({ event, data } = {}) => {
  
    if (this.events[event] && typeof this.events[event] === 'function') {
      this.events[event](data);
    }
  };

  onMessageCreated = message => {
    store.dispatch(addMessageToConversation({ message }));
  };


}

export async function initActionCable() {
  const pubSubToken = await getPubSubToken();

  const actionCable = new ActionCableConnector(pubSubToken);
  return actionCable;
}

Root Component

import React, { Component } from 'react';
import { initActionCable } from '../../helpers/ActionCable';

class ConversationList extends Component {

  componentDidMount = () => {
    initActionCable();
  };
  render = () => {
    return (
      <Layout>
      </Layout>
    );
  };
}

export default ConversationList;

The connection to action cable is working here. But the problem is that whenever I tried to restart the app, It got connected to action cable again. So multiple events are firing. Is there any way to check the connection is already established or not?

muhsin-k avatar Feb 16 '20 14:02 muhsin-k

@kesha-antonov Could you please help me with this?

muhsin-k avatar Feb 16 '20 16:02 muhsin-k

Hi! I need to take a look

Did you try disconnect on unmount?

kesha-antonov avatar Feb 17 '20 04:02 kesha-antonov

@kesha-antonov I have tried. But no luck at all.

Here is the action cable connector helper.

https://github.com/chatwoot/chatwoot-mobile-app/blob/34-chat-screen/src/helpers/ActionCable.js

Here I am using it, https://github.com/chatwoot/chatwoot-mobile-app/blob/34-chat-screen/src/screens/ConversationList/ConversationList.js#L71

muhsin-k avatar Feb 17 '20 04:02 muhsin-k

Did you try to call

connectActionCable.disconnect()

on unmount?

And also to unsubscribe from all channels?

kesha-antonov avatar Feb 17 '20 04:02 kesha-antonov

Yes

muhsin-k avatar Feb 17 '20 05:02 muhsin-k

@muhzi4u Did you find a solution for this?

cdesch avatar Mar 01 '20 21:03 cdesch

@cdesch No. I couldn't. Are you facing the same issue?

muhsin-k avatar Mar 02 '20 05:03 muhsin-k

@muhzi4u

...whenever I tried to restart the app... Is it related to Fast Refresh?

kesha-antonov avatar Mar 02 '20 07:03 kesha-antonov

@kesha-antonov It is happening production version too.

muhsin-k avatar Mar 02 '20 10:03 muhsin-k

@muhsin-k Did you ever solve this? I am having the same issue right now. Local Development is fine, production I get every message multiple times.

marvwhere avatar Jan 04 '23 14:01 marvwhere

Any updates here? Did you solve this? @marvwhere

DSKonstantin avatar Jul 05 '24 09:07 DSKonstantin