DoneJS StealJS jQuery++ FuncUnit DocumentJS
3.14.1
5.0.0 4.3.0 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-compute
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-stream
      • can-define-stream-kefir
      • can-event
      • can-event/async/async
      • can-event/batch/batch
      • can-event/lifecycle/lifecycle
      • can-kefir
      • can-list
      • can-map
      • can-map-backup
      • can-map-define
      • can-observation
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
    • Data Modeling
      • can-connect
      • can-connect-cloneable
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-signalr
        • options
          • signalR
        • methods
          • init
        • data interface
          • createData
          • destroyData
          • getData
          • getListData
          • updateData
      • can-fixture
      • can-fixture-socket
      • can-ndjson-stream
      • can-set
    • Views
      • can-component
      • can-ejs
      • can-element
      • can-react-component
      • can-stache
      • can-stache/helpers/route
      • can-stache-bindings
      • can-stache-converters
      • can-view-autorender
      • can-view-callbacks
      • can-view-href
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • react-view-model
      • react-view-model/component
      • steal-stache
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-globals
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-string
      • can-string-to-any
      • can-util
      • can-zone
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-control
      • can-dom-events
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-jquery
    • Data Validation
      • can-define-validate-validatejs
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-namespace
      • can-reflect
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

can-connect-signalr

  • npm package badge
  • Star
  • Edit on GitHub

Connect to a Hub on a SignalR server.

connectSignalR( baseBehavior )

Encapsulates connecting to a SignalR hub, by:

  • implementing the: createData, updateData, getData, getListData, and destroyData DataInterface methods to make RPC calls to the server.

  • listening for the following messages pushed from the server to the browser:

    • signalR.createdName,
    • signalR.updatedName,
    • signalR.destroyedName

    and calling: createInstance, updateInstance, or destroyInstance.

Use

can-connect-signalr is a can-connect behavior that makes a connection that can communicate with a Hub on a SignalR server.

The following walks through an example setup that allows a Message type to be created, retrieved, updated and deleted by the client AND to be notified when messages are created, updated, or deleted by the server.

Specifically, we will detail the:

  • can-connect Client setup
  • Hub Interface Requirements

can-connect Client setup

Below is a complete example of connecting a DefineMap model type to a SignalR hub:

var DefineMap = require('can-define/map/map');
var DefineList = require('can-define/list/list');
var connect = require("can-connect");

// Defines the Type that will be used on the client.
var Message = DefineMap.extend({
    body: 'string',
    id: 'number'
});

// Defines a List type that contains instances of the
// Type.
Message.List = DefineList.extend({
    '#': Message
});

// The minimal behaviors used to create the connection
var behaviors = [
    require('can-connect/constructor/constructor'),
    require('can-connect/constructor/store/store'),
    require('can-connect/can/map/map'),
    require('can-connect/data/callbacks/callbacks'),
    require('can-connect/real-time/real-time'),
    require('can-connect/constructor/callbacks-once/callbacks-once'),
    require('can-connect-signalr') // Import the signalR Behavior
];

// Connects the types to the SignalR server
Message.connection = connect(behaviors, {
    Map: Message,
    List: Message.List,
    signalR: {
        url: 'http://test.com',
        name: 'MessageHub'
    }
});

This example creates a Message can-define/map/map type and Message.List can-define/list/list type and connects them to MessageHub at http://test.com.

This sets up Message so it can retrieve, create, update and delete Messages as follows:

Method Description Details
Message.getList({due: "today"});
retrieves a list of messages This calls MessageHub's public List messageHubGetListData(MessageQueryParams queryParams) method which is expected to return a list of matched messages.
Message.get({id: 5});
gets a single message This calls MessageHub's public MessageModel messageHubGetData( int id ) method which is expected to return a single message.
var message = new Message({
  body: "Hello World!"
}).save();
creates messages This calls MessageHub's public MessageModel messageHubCreate( MessageModel message ) method with the serialized properties of the client message. MessageHubCreate is expected to persist the message, add a unique id property and value, and return the Message's new data. It should also notify clients that a message was created.
message.body = "Hi there."; 
message.save();
updates a message This calls MessageHub's public MessageModel messageHubUpdate( MessageModel message ) method which is expected to update the persisted representation of the message and return the Message's new data. It should also notify clients that a message was updated.
message.destroy();
deletes a message This calls MessageHub's public MessageModel messageHubDestroy( MessageModel message ) method which is expected to delete the persisted representation of the message and return the Message's updated data. It should also notify clients that a message was destroyed.

Hub Server Setup

The following code outlines a MessageHub that would work with with the above client setup:

public class MessageHub : Hub
    {

        public MessageHub(MyRepository repository)
        {
        }

        // Method should take whatever data is required to create an instance
        public MessageModel MessageHubCreate( MessageModel message )
        {
            PERSIST_TO_DATABASE( message );

            message.id // type must have a unique id property

            // Any RPC calls to the client related to creation go here
            Clients.All.messageHubCreated(message);
            return message;
        }

        // Method should take whatever data is required to update an instance
        public MessageModel MessageHubUpdate( MessageModel message )
        {
            UPDATE_DATABASE( message );

            // Any RPC calls to the client related to update go here
            Clients.All.messageHubUpdated(message);
            return message;
        }

        // Method should take whatever data is required to destroy an instance (usually an id)
        public MessageModel MessageDestroy( MessageModel model )
        {
            DELETE_FROM_DATABASE( model );

            // Any RPC calls to the client related to destroy go here
            Clients.All.messageDestroyed(model);
        }

        // Method should take whatever data is required to obtain a list (if any)
        public List<MessageModel> MessageGetList( MessageQueryParams queryParams )
        {
            List<MessageModel> messages = GET_DATA_FROM_DATABASE( queryParams );
            return messages;
        }

        // Method should take whatever data is required to obtain a specific item
        public MessageModel MessageGet( int id )
        {
            MessageModel message = GET_RECORD_FROM_DATABASE( id );

            return message;
        }

        ...
    }

Configuration

The name of the Hub is specified by signalR.name. This is used to create default method and event names.

For example, if the signalR.name is "TaskHub", it will make RPC calls for the following methods (signalR configuration name in parenthesis):

  • taskHubGetData (signalR.getListName)
  • taskHubGetListData (signalR.getListName)
  • taskHubCreateData (signalR.createName)
  • taskHubUpdateData (signalR.updateName)
  • taskHubDestroyData (signalR.destroyName)

It will listen to the following events (signalR configuration name in parenthesis):

  • taskHubCreatedData (signalR.createdData)
  • taskHubUpdatedData (signalR.updatedData)
  • taskHubDestroyedData (signalR.destroyedData)

For example, you can overwrite these defaults like:

connect(behaviors,{
    Map: Task,
    signalR: {
        url: "/hubs",
        name: "TaskHub",

        // Calls TaskHub.getList() instead of TaskHub.taskHubGetListData().
        getListName: "getList",
    }
});

CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.14.1.

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news