Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onReceived is never called #19

Open
patrickemuller opened this issue Nov 16, 2018 · 7 comments
Open

onReceived is never called #19

patrickemuller opened this issue Nov 16, 2018 · 7 comments

Comments

@patrickemuller
Copy link

What I'm trying to do?

I'm trying to use the backend with some background jobs to notify user about the "processing" time of some CSV file. For that, I'm notifying the frontend about the processed rows of the file, but, onReceived function is never called. What I'm doing wrong?

Backend

(this is working, tested manually)

# frozen_string_literal: true

class ImportedBatchChannel < ApplicationCable::Channel
  def subscribed
    stream_for ImportedBatch.find(params[:room])
  end

  def receive(data)
    puts '*****************************'
    puts data
    puts '*****************************'
  end
end

Frontend

(this is not working, OnReceived is never called)

App.js (REACT_APP_WS_URL is localhost:3001/cable for development)

<ActionCableProvider url={process.env.REACT_APP_WS_URL}>
  <MuiThemeProvider theme={theme}>
    <CssBaseline />
    <Navbar />
    <Router />
    <Footer />
  </MuiThemeProvider>
</ActionCableProvider>

ProgressBar.js

import React, { Component, Fragment } from 'react'
import { ActionCable } from 'react-actioncable-provider'
import LinearProgress from '@material-ui/core/LinearProgress'

export default class extends Component {
  state = {
    total: this.props.total,
    processed: this.props.processed,
  }

  onReceived(data) {
    console.log('Recebi', data)
    this.setState({
      total: data.totalRows,
      processed: data.processedRows,
    })
  }

  sendMessage = () => {
    this.refs.ImportedBatchChannel.perform('receive', { message: 'olha essa mensagem' })
  }

  render() {
    return (
      <Fragment>
        <ActionCable
          ref={'ImportedBatchChannel'}
          channel={{
            channel: 'ImportedBatchChannel',
            room: this.props.batchId,
          }}
          onReceived={this.onReceived}
        />

        <div className={this.props.classes.progressBar}>
          <LinearProgress
            variant="determinate"
            value={(this.state.processed / this.state.total) * 100}
          />
        </div>
        <button onClick={this.sendMessage}>Mandar Mensagem</button>
      </Fragment>
    )
  }
}
@wh1le
Copy link

wh1le commented Nov 28, 2018

Same problem

@patrickemuller
Copy link
Author

@nmiloserdov check if you're using "async" on backend and change to "redis". Worked for me.
but yeah, I think its bugged anyway.

@wh1le
Copy link

wh1le commented Nov 28, 2018

@patrickemuller I use 'redis' by default. What do you mean by using "async" on the backend?

@patrickemuller
Copy link
Author

There is and option on cable.yml that solved my problem, using redis instead os 'async' adapter.
you're using redis already, so there is nothing to change.

@wh1le
Copy link

wh1le commented Nov 29, 2018

@patrickemuller I found the problem. ActilnCable rejected my connection because of the custom validation of the user id. onConnected and onDisconnected callbacks helped me to figured this out.

@wh1le
Copy link

wh1le commented Nov 29, 2018

@cpunion you just need to describe these methods on documentation :)

@patrickemuller
Copy link
Author

@nmiloserdov @cpunion Also, I added .bind for those methods on my frontend code. I think that was missed from the beginning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants