import EmbarkJS from 'Embark/EmbarkJS'; import React from 'react'; import {Alert, Form, FormGroup, Input, Button, FormText} from 'reactstrap'; class Whisper extends React.Component { constructor (props) { super(props); this.state = { listenTo: '', channel: '', message: '', subscribedChannels: [], channelIsValid: false, listenToChannelIsValid: false, messageList: [], logs: [] }; } handleChange (e, name) { this.state[name] = e.target.value; if (name === 'listenTo') { this.state.listenToChannelIsValid = this.isChannelValid(e.target.value); } else if (name === "channel") { this.state.channelIsValid = this.isChannelValid(e.target.value); } this.setState(this.state); } checkEnter(e, func) { if (e.key !== 'Enter') { return; } e.preventDefault(); func.apply(this, [e]); } sendMessage (e) { e.preventDefault(); EmbarkJS.Messages.sendMessage({topic: this.state.channel, data: this.state.message}); this.addToLog("EmbarkJS.Messages.sendMessage({topic: '" + this.state.channel + "', data: '" + this.state.message + "'})"); } listenToChannel (e) { e.preventDefault(); const subscribedChannels = this.state.subscribedChannels; subscribedChannels.push(this.state.listenTo); this.setState({ subscribedChannels }); const messageList = this.state.messageList; EmbarkJS.Messages.listenTo({topic: [this.state.listenTo]}).subscribe( message => { messageList.push(Channel: {message.topic} | Message: {message.data}); this.setState({messageList}); }, error => { messageList.push(Error: {error.message || "Unknown Error"}); this.setState({messageList}); } ); this.addToLog("EmbarkJS.Messages.listenTo({topic: ['" + this.state.listenTo + "']}).then(function(message) {})"); } addToLog (txt) { this.state.logs.push(txt); this.setState({logs: this.state.logs}); } isChannelValid(name) { return name.length >= 4; } render () { return ( { !this.props.enabled && The node you are using does not support Whisper The node uses an unsupported version of Whisper }

Listen To channel

this.checkEnter(e, this.listenToChannel)}> this.handleChange(e, 'listenTo')}/> {!this.state.listenToChannelIsValid && Channel has to be at least 4 characters long}
{this.state.subscribedChannels.map((item, i) =>

Subscribed to {item}. Now try sending a message

)}
Messages received:
{this.state.messageList.map((item, i) =>

{item}

)}

Send Message

this.checkEnter(e, this.sendMessage)}> this.handleChange(e, 'channel')}/>
this.handleChange(e, 'message')}/>

Javascript calls being made:

EmbarkJS.Messages.setProvider('whisper')

{ this.state.logs.map((item, i) =>

{item}

) }
); } } export default Whisper;