diff --git a/embark-ui/src/components/Communication.js b/embark-ui/src/components/Communication.js
index 27f974cd..b81a36c3 100644
--- a/embark-ui/src/components/Communication.js
+++ b/embark-ui/src/components/Communication.js
@@ -10,7 +10,6 @@ class Communication extends Component {
listenTo: '',
channel: '',
message: '',
- subscribedChannels: [],
messageList: []
};
}
@@ -28,13 +27,6 @@ class Communication extends Component {
listenToChannel(e) {
e.preventDefault();
-
- const subscribedChannels = this.state.subscribedChannels;
- subscribedChannels.push(this.state.listenTo);
- this.setState({
- subscribedChannels
- });
-
this.props.listenToMessages(this.state.listenTo);
}
@@ -52,11 +44,11 @@ class Communication extends Component {
- {this.state.subscribedChannels.length > 0 &&
+ {this.props.subscriptions && this.props.subscriptions.length > 0 &&
Subscribed channels:
- {this.state.subscribedChannels.map((item, i) => {item})}
+ {this.props.subscriptions.map((item, i) => {item})}
}
@@ -107,6 +99,7 @@ class Communication extends Component {
Communication.propTypes = {
sendMessage: PropTypes.func,
listenToMessages: PropTypes.func,
+ subscriptions: PropTypes.array,
channels: PropTypes.object
};
diff --git a/embark-ui/src/containers/CommunicationContainer.js b/embark-ui/src/containers/CommunicationContainer.js
index 6a1b2e5a..e13e6700 100644
--- a/embark-ui/src/containers/CommunicationContainer.js
+++ b/embark-ui/src/containers/CommunicationContainer.js
@@ -34,7 +34,8 @@ class CommunicationContainer extends Component {
{isEnabledMessage}
this.listenToChannel(channel)}
sendMessage={(channel, message) => this.sendMessage(channel, message)}
- channels={this.props.channels}/>
+ channels={this.props.channels}
+ subscriptions={this.props.subscriptions}/>
);
}
@@ -45,12 +46,14 @@ CommunicationContainer.propTypes = {
messageListen: PropTypes.func,
communicationVersion: PropTypes.func,
channels: PropTypes.object,
+ subscriptions: PropTypes.array,
version: PropTypes.number
};
function mapStateToProps(state) {
return {
channels: state.messages.channels,
+ subscriptions: state.messages.subscriptions,
version: state.messages.version
};
}
diff --git a/embark-ui/src/reducers/messagesReducer.js b/embark-ui/src/reducers/messagesReducer.js
index e3857ea9..dac9ef3d 100644
--- a/embark-ui/src/reducers/messagesReducer.js
+++ b/embark-ui/src/reducers/messagesReducer.js
@@ -16,6 +16,14 @@ export default function messages(state = {channels: {}}, action) {
}
};
}
+ case actions.MESSAGE_LISTEN[actions.REQUEST]: {
+ const subscriptions = state.subscriptions || [];
+ subscriptions.push(action.channel);
+ return {
+ ...state,
+ subscriptions: subscriptions
+ };
+ }
case actions.MESSAGE_VERSION[actions.SUCCESS]: {
return {
...state,
diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js
index 49e7a317..1730466d 100644
--- a/lib/modules/whisper/index.js
+++ b/lib/modules/whisper/index.js
@@ -148,7 +148,7 @@ class Whisper {
'ws',
'/embark-api/communication/listenTo/:topic',
(ws, req) => {
- listenTo({
+ self.webSocketsChannels[req.params.topic] = listenTo({
topic: req.params.topic,
messageEvents,
toHex: self.web3.utils.toHex,
@@ -157,6 +157,9 @@ class Whisper {
symKeyID,
subscribe: self.web3.shh.subscribe
}, (err, result) => {
+ if (ws.readyState === ws.CLOSED) {
+ return;
+ }
if (err) {
return ws.status(500).send(JSON.stringify({error: err}));
}
diff --git a/lib/modules/whisper/js/communicationFunctions.js b/lib/modules/whisper/js/communicationFunctions.js
index 5f5a2099..c265c895 100644
--- a/lib/modules/whisper/js/communicationFunctions.js
+++ b/lib/modules/whisper/js/communicationFunctions.js
@@ -99,7 +99,8 @@ function listenTo(options, callback) {
return callback(null, data);
}
promise.cb(payload, data, result);
- });
+ })
+ .catch(callback);
return promise;
}