import EmbarkJS from 'Embark/EmbarkJS'; import React from 'react'; import {Alert, Form, FormGroup, FormControl, Button} from 'react-bootstrap'; class Whisper extends React.Component { constructor (props) { super(props); this.state = { listenTo: '', channel: '', message: '', subscribedChannels: [], messageList: [], logs: [] }; } handleChange (e, name) { this.state[name] = 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(Subscribed to {this.state.listenTo}. Now try sending a message); this.setState({ subscribedChannels }); EmbarkJS.Messages.listenTo({topic: [this.state.listenTo]}, (error, message) => { const messageList = this.state.messageList; if (error) { messageList.push(Error: {error}); } else { messageList.push(Channel: {message.topic} | Message: {message.data}); } 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}); } 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.subscribedChannels.map((item, i) =>

{item}

)}

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;