SignalR (Push Services with Hubs)–Part 1

ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of adding real-time web functionality to applications. Real-time web functionality is the ability to have server code push content to connected clients instantly as it becomes available, rather than having the server wait for a client to request new data.
  • Push services is something like whenever data changes in any of the devices (web, mobile, ssms) the data displayed on other devices will be updated instantly.
    Push Pattern:
    • Push services are not an official pattern.
    • Technical approaches for push
      • Periodical polling
      • Long polling
      • Forever Frame
      • Service Sent Events (SSE)
      • Web Sockets
    SignalR:
    • A server side framework to write push services
    • A set of client libraries to make push services communication easy to use on any platform
    • Optimized for asynchronous processing
    • Two Programming Models
      • Persistent connections
      • Hubs
    • NOTE: SignalR packages are available through NuGet packages
    SignalR Hubs:
    • Hubs are classes to implement push services in SignalR
      • Abstraction on top of persistent connection
      • Convention-over-configuration
    • Hubs provide a high level RPD framework
      • Perfect for different type of messages to send between server and client
    • Hub Conventions
      • Public methods are callable from the outside
      • Send messages to the clients by invoking client-side methods
    • Simple steps to get going
      • Write hub class
      • Add route in host process
      • Done
    • Hub Class:
      • [HubName] attribute is used to reflect the Name of Hub in API
      • Can return Simple Type or Complex Type Or Task
      • Complex objects and arrays are automatically serialized/deserialized to/from JSON
      • Context holds connection and request specific information like ConnectionId, Request, headers, Query string, User and Request Cookies
    • Hubs Protocol
      • Base endpoint is /signalr
      • Optional: get JS meta data from /signalr/hubs
      • Basically two protocol steps(details vary by transport)
        • /negotiate – which transport do you support?
        • (Optional: /ping server)
        • /connect – Okay. Here is my persistent connection.
      • 'BEST' transport negotiation( Try to open a connection with below options, if one fails it will try for next tech)
        • Web Sockets -> SSE -> Forever Frame -> Long Polling
      • Pre-defined pay load
      • Any data is JSON encoded
    • Pushing Data: Clients
      • Clients property as dispatching point to send messages to clients
      • Holds dynamic properties and methods
      • Target methods with parameters are dynamically injected into code path, serialized, and embedded into protocol response.
    • Pushing Data: Groups
      • Typical base pattern in push scenarios
      • Can add connections to group and send messages to particular groups
      • Groups are not persisted on the server
        • We need to keep track of what connections are in what group
        • No automatic group count
    • Hub Lifecycle
      • Hub Connections have a     life cycle
        • Override Async pipeline event handles( OnConnected(), OnDisconnected(), OnReconnected())
        • Sending data from outside a hub
          • Retrieve hub context via dependency resolver
    • Sample:
      • Create Service:
        • Need to add Hub Class.
        • Need to add RouteTables.Routes.MapHubs();
        • Create method with simple type or complex type or Task.
        • The Response in browser will show java script code which builds the proxy class what we mentioned in Hub Class.
        • The request will contain the message, hub name, client method name in JSON format. (Request details can be only viewable in ling-polling transport approach.

Comments