Skip to content
Anders Malmgren edited this page Mar 6, 2019 · 1 revision

React Client

Install using unpkg signalr.eventaggregatorproxy.react as decribed here

Requires the vanilla client library too, so do not forget to install it. Also do not forget to register the event proxy.

<script src="/eventAggregation/events" asp-append-version="true"></script>

Install plugin

The plugin is implemented as a Higher-Order Component or HOC. You need to extend your Component. Its done like

const SignalrMyComponent = withSignalREventAggregator(MyComponent);

Use the plugin

From your component

componentDidMount() {
    this.props.subscribe(MyApp.MyEvent, this.onEvent);
}

onEvent(e) {
    this.setState(state => ({
        events: state.events.concat(e)
    }));
}

You can use all of the vanilla client overloads, the context (this) is marshaled by React. This means you do not need to unsubscribe to messages even in a SPA scenario. The component will call unsubscribe on the componentWillUnmount event for you.

Publish client side events

Just like the vanilla client you can publish events.

this.props.publish(new MyClientSideEvent());