Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


This hook does two things:

  1. It provides an implementation of the above interface that can be used with
  2. Provides a set of methods and dispatchers that can be used by default with any model in the application (using some overridable conventions)


Implementing support for is fairly straight forward. The implementation just needs to know the serverUrl (which is the root URL for the server), the namespace (if your server uses one) and the event that will be emitted when CRUD operations occur for the desired resource.

var io = require('');
var WebSocketConnection = require('lore-websockets').WebSocketConnection;

module.exports = WebSocketConnection.extend({

  // These three values are provided by the project configuration or conventions
  serverUrl: 'http://localhost:1337',
  namespace: '/posts',
  event: 'post',

  connect: function() {
    var url = this.serverUrl + this.namespace;
    this.socket = io(url);

  subscribe: function() {
    this.socket.on(this.event, this.dispatch);

  unsubscribe: function() {, this.dispatch);


Default Methods and Dispatchers

The code below illustrates the general setup process used when the hook creates the WebSocket instance:

// these "guess" the namespace and event based on conventions
// can be provided explicitly by the user
var conventions = {
  namespace: config.pluralize ? `/${pluralize(modelName)}` : `/${modelName}`,
  event: modelName

// these three dispatchers are provided by default to update the Redux store
// based on data that was created, updated or deleted by other users.
var dispatchers = {
  created: blueprints.dispatchers.created(modelName, Model)(store),
  updated: blueprints.dispatchers.updated(modelName, Model)(store),
  destroyed: blueprints.dispatchers.destroyed(modelName, Model)(store)

// override the SocketIo WebSocketConnection with conventions and configuration
var CustomWebSocketConnection = SocketIoWebSocketConnection.extend(_.extend(conventions, config));

// make the connection accessible under lore.websockets, i.e. for example.
lore.websockets[modelName] = new CustomSocketIoWebSocketConnection(dispatchers);

If you want to listen for events during the entire lifecycle of your application, a good place to connect and listen for data is within the componentDidMount method of the Master component, like so:

// src/components/Master.js
  componentDidMount: function() {;;

  componentWillUnmount: function() {;

Calling will cause the websocket instance to connect with the server. Calling will cause it to listen for the event it was configured for, such as events called post that contain data about Post resources that have been created, updated or deleted by other users.