Pub/Sub High-level Components

TopicPublisher

This component will publish messages to the specified topic.

To use it, wrap it around your presentational component. The function this.props.publish will be passed down, and you can use it to publish to the topic.

SendMessage.jsx

import React from 'react';


class SendMessage extends React.Component {
  onPressedKey(e) {
    if (e.charCode === 13) { // Enter key
      e.preventDefault();
      let node = this.refs.msgInput;
      this.props.publish({ args: [node.value] });
      node.value = '';
    }
  }
  render() {
    return (
      <input ref="msgInput" onKeyPress={() => this.onPressedKey()} type="text" />
    );
  }
});

SendMessage.propTypes = {
  publish: React.PropTypes.func,
};

export default SendMessage;

App.jsx

import React from 'react';

import TopicPublisher from 'simpl-react/lib/components/TopicPublisher.react';

import SendMessage from '../components/SendMessage';


const App = function App() {
  return (
    <div>
      <h3>Send messages</h3>
      <TopicPublisher topic="com.example.sims.simpl-calc.chat">
        <SendMessage />
      </TopicPublisher>
    </div>
  );
};

export default App;

Props

TopicSubscriber

Notifications.jsx

import React from 'react';

const Notification = function Notifications(message) {
  return (<li key={message.kwargs.publication}>{message.args[0]}</li>)
};

const Notifications = function Notifications(props) {
  let messages;

  if (props.data !== undefined) {
    messages = props.data.map(Notification);
  }

  return (
    <div>
      <ul>{messages}</ul>
    </div>
  );
};

export default Notifications;

App.jsx

import React from 'react';

import TopicPublisher from 'simpl-react/lib/components/TopicPublisher.react';

import Notification from '../components/Notification';


const App = function App() {
  return (
    <div>
      <h3>Chat</h3>
      <TopicSubscriber topic="com.example.sims.simpl-calc.chat">
        <Notification />
      </TopicSubscriber>
    </div>
  );
};

export default App;

Props