Skip to content

Instantly share code, notes, and snippets.

@ugened47
Created April 18, 2020 11:24
Show Gist options
  • Save ugened47/44a1f4f3f557f8954bf5d7c86cb2f728 to your computer and use it in GitHub Desktop.
Save ugened47/44a1f4f3f557f8954bf5d7c86cb2f728 to your computer and use it in GitHub Desktop.
RN-actioncable
`yarn add actioncable`
import ActionCable from 'react-native-actioncable'
const cable = ActionCable.createConsumer('ws://localhost:3000/cable');
export default class Room extends Component {
componentDidMount () {
this.subscription = cable.subscriptions.create(
'ChannelName',
{
received (data) {
console.log(data)
},
disconnect () {
console.log('disconnected')
},
received (data) {
console.log(data)
}
}
)
}
componentWillUnmount () {
this.subscription && cable.subscriptions.remove(this.subscription)
}
handleSend = (e) => {
const data = {
body: { action: 'DriverLocationUpdated', message: e.target.value; }
}
const message = {
command: 'message',
identifier: JSON.stringify({
channel: 'ChannelName'
}),
data: JSON.stringify(data)
}
socket.send([message])
}
render() {
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment