Created
April 18, 2020 11:24
-
-
Save ugened47/44a1f4f3f557f8954bf5d7c86cb2f728 to your computer and use it in GitHub Desktop.
RN-actioncable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
`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