ASP.NET SignalR SoftUni Team Technical Trainers Software University

Slides:



Advertisements
Similar presentations
Nikola Dimitroff Creating Genres creatinggenres.com.
Advertisements

Building Real Time Applications with ASP.NET SignalR 2.0
JavaScript Basics Course Introduction SoftUni Team Technical Trainers Software University
AngularJS Routing Routes, Route Parameters, Templates, Location, Navigation SoftUni Team Technical Trainers Software University
AngularJS Services Built-in and Custom Services SoftUni Team Technical Trainers Software University
Software University Curriculum, Courses, Exams, Jobs SoftUni Team Technical Trainers Software University
Fundamentals SoftUni Welcome to Software University SoftUni Team Technical Trainers Software University
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC SoftUni Team Technical Trainers Software University
AngularJS Directives Defining Custom Directives SoftUni Team Technical Trainers Software University
Software Testing Lifecycle Exit Criteria Evaluation, Continuous Integration Ivan Yonkov Technical Trainer Software University.
Teamwork and Personal Skills Course Introduction Software University SoftUni Team Technical Trainers.
Design Patterns: Structural Design Patterns
High-Quality Programming Code Code Correctness, Readability, Maintainability, Testability, Etc. SoftUni Team Technical Trainers Software University
Composer packages Installing and Using Composer, Packagist, Packaging your code Mario Peshev Technical Trainer Software University
Consuming REST Services from C# SoftUni Team Technical Trainers Software University
Start Your Own Blog Angel Georgiev Part-time Trainer angeru.softuni-friends.org Software University The Culture of Knowledge Sharing.
Entity Framework Performance SoftUni Team Technical Trainers Software University
Microsoft Azure SoftUni Team Technical Trainers Software University
Svetlin Nakov Technical Trainer Software University
Build Processes and Continuous Integration Automating Build Processes Software University Technical Trainers SoftUni Team.
Processing Redis with.NET How to Operate with Redis Databases SoftUni Team Technical Trainers Software University
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC Angel Georgiev Part-time Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding SoftUni Team Technical Trainers Software University
Trainers Team Ivan Yonkov Rated in the top 7% at Stack Overflow
AMD and RequireJS Splitting JavaScript Code into Dependent Modules Software University Technical Trainers SoftUni Team.
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University
Controls, Widgets, Grid…
Jekyll Static Site Generator Template-Based Site Generation Svetlin Nakov Technical Trainer Software University
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
Responsive Design Design that Adapts to Different Devices SoftUni Team Technical Trainers Software University
Processing JSON in.NET JSON, JSON.NET LINQ-to-JSON and JSON to XML SoftUni Team Technical Trainers Software University
High-Quality Programming Code Code Correctness, Readability, Maintainability Svetlin Nakov Technical Trainer Software University
High-Quality Code: Course Introduction Course Introduction SoftUni Team Technical Trainers Software University
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
Advanced C# Course Introduction SoftUni Team Technical Trainers Software University
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
Object-Oriented Programming Course Introduction Svetlin Nakov Technical Trainer Software University
JavaScript Applications Course Introduction SoftUni Team Technical Trainers Software University
Mocking with Moq Tools for Easier Unit Testing SoftUni Team Technical Trainers Software University
ASP.NET Identity System
Mocking Unit Testing Methods with External Dependencies SoftUni Team Technical Trainers Software University
Mocking with Moq Mocking tools for easier unit testing Svetlin Nakov Technical Trainer Software University
JavaScript Tools Tools for Writing / Editing / Debugging JavaScript Code Svetlin Nakov Technical Trainer Software University
ORM Basics Repository Pattern, Models, Entity Manager Ivan Yonkov Technical Trainer Software University
Sets, Dictionaries SoftUni Team Technical Trainers Software University
JavaScript Applications Course Introduction SoftUni Team Technical Trainers Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
High-Quality Code: Course Introduction Course Introduction SoftUni Team Technical Trainers Software University
Software Technologies Course Overview SoftUni Team Technical Trainers Software University
Programming Fundamentals Course Introduction SoftUni Team Technical Trainers Software University
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Team Work and Personal Skills Course Introduction Angel Georgiev Part-time Trainer Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
ASP.NET MVC Course Program, Trainers, Evaluation, Exams, Resources SoftUni Team Technical Trainers Software University
Web Storage and Cookies Cookies, Local and Session Storage SoftUni Team Technical Trainers Software University
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Advanced JavaScript Course Introduction SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Generics SoftUni Team Technical Trainers Software University
Auto Mapping Objects SoftUni Team Database Applications
Web API - Introduction AJAX, Spring Data REST SoftUni Team Web API
Introduction to MVC SoftUni Team Introduction to MVC
Deploying Web Application
ASP.NET Integration Testing
Mocking tools for easier unit testing
ASP.NET SignalR SoftUni Team C# MVC Frameworks Technical Trainers
Extending functionality using Collections
Manual Mapping and AutoMapper Library
Presentation transcript:

ASP.NET SignalR SoftUni Team Technical Trainers Software University

2  Web-based real-time communication  Problems and solutions  ASP.NET SignalR - Overview & concepts  Hubs  Server-side API  Clients  Client-side API  Chat Live Demo Table of Contents

Web based real-time communication Now, now and now!

4  Users want data  Now and instant  Up-to-date  Delivered to any device, over any connection  Examples  Live searches / updates  Stock streamers, auctions  Live scores, betting, interactive games  Real-time feedback, real-time notifications Problems & Solutions

5  Developers need to provide real time data  But not only for web applications  What about mobile devices & apps?  What about traditional desktop applications?  What about server-to-server?  Push communication beyond the web is a need  Think, design & implement Push Services Problems & Solutions

Push Services Pattern Not official of course!

7  Push Services are not an official pattern  Model a service that  accepts incoming connections from callers  is able to push data down to callers  abstracts from communication only details Push Services Pattern

8  Web communication – HTTP!  Basically request-response  Realize Push services with HTTP  Periodic Polling  Long Polling  Forever Frame  Server-Sent Events (SSE)  Web Sockets  HTTP Protocol

9  Web Sockets – latest piece of technology!  Positives:  Easy  Constant connection  Send only small details  Negatives  Only with Windows 8 / Server 2012  Network considerations  Maybe some time, but not today Web Sockets

Live Demo

SignalR Part of ASP.NET but not tied to it

12  SignalR is:  A server-side framework to write push services  Clients for easy communication on any device  Optimized for asynchronous operations  Operation:  Persistent connection  Data is sent as signal through connection ID  Volatile SignalR

13  Framework is based on interfaces:  IConnection, IHub, etc.  Programming modes:  Persistent connection  Hubs  Hubs offer predefined API for most scenarios  SignalR example:  SignalR

14  Packages at NuGet:  Microsoft.AspNet.SignalR  Microsoft.AspNet.SignalR.Core  Microsoft.AspNet.SignalR.Owin  Microsoft.AspNet.SignalR.Js  Microsoft.AspNet.SignalR.Client  Microsoft.AspNet.SignalR.Utils SignalR

15  Installation  Download Microsoft.AspNet.SignalR  In Startup.cs file map the SignalR  Create directory Hubs  Add new SignalR classes to it  Done! SignalR public void Configuration(IAppBuilder app) { app.MapSignalR(); app.MapSignalR();}

SignalR Hubs Server-side

17  Classes to implement push services  Abstraction on top of persistent connection  Convention over configuration  Perfect for sending data from server to clients  Conventions  Public methods are callable from clients  Send data by invoking client methods Hubs

18  Hub name reflected onto external API  Return simple type, complex type or Task  Objects and collection – automatically to JSON  Context  ConnectionId  Request  Headers  RequestCookies  QueryString  User Hub Programming

19  Base endpoint is /signalr  JS metadata from /signalr/hubs  Basically two protocol steps  negotiate: which transport do you support?  connect: OK, here is my persistent connection  "Best" transport negotiation  W. Sockets -> SSE -> Forever frame -> L. polling  Any data is JSON encoded Hub Programming

20  Clients property to send messages to clients  Holds dynamic properties and methods  Target method with parameters  Dynamically "injected"  Serialized  Embedded into response Clients Property public void SendMessage(string message) { var messageToSend = string.Format("{0}: {1}", Context.ConnectionId, message); var messageToSend = string.Format("{0}: {1}", Context.ConnectionId, message); Clients.All.newMessage(messageToSend); Clients.All.newMessage(messageToSend);}

21  Groups - typical base pattern in push scenarios  Add connections to groups  And send messages to particular groups  Groups are not persisted on server! Groups Property public void JoinRoom(string room) { Groups.Add(Context.ConnectionId, room); Groups.Add(Context.ConnectionId, room);} public void SendToRoom(string room, string message) { var msg = string.Format("{0}: {1}", Context.ConnectionId, message); var msg = string.Format("{0}: {1}", Context.ConnectionId, message); Clients.Group(room).newMessage(msg); Clients.Group(room).newMessage(msg);}

SignalR Clients Client-side

23  Consumers  Client applications  Other services/hubs  SignalR provides a variety of client libraries  WinRT  Windows Phone 8  Silverlight 5  jQuery  C++ Clients

24  Automatic proxy code via /signalr/hubs  Script generated based on hubs declaration  Hubs become properties on $.connection  Example $.connection.chatHub  Hub name should be camel-cased  $.connection.hub.start() – start connection jQuery Client $.connection.hub.start({ transport: 'longPolling'}); var chat = $.connection.chat; chat.server.joinRoom('private');

25  Define client side methods  They can be invoked by the hub  Events for connection state handling  Detect slow connections  Cross-domain support jQuery Client var chat = $.connection.chat; chat.client.newMessage = onNewMessage; function onNewMessage(message) { $('#messages').append(message); $('#messages').append(message); }

Chat Live Demo

27  Web sockets provide persistent real-time communication  Push services  Don’t ask us, we’ll call you when we have something for you  ASP.NET SignalR  Provides an abstraction over push services  Part of ASP.NET, but can be used separately  Provides a unified API for C# and JavaScript code Summary

? ? ? ? ? ? ? ? ? ASP.NET MVC SignalR

SoftUni Diamond Partners

License  This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" licenseCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International 30  Attribution: this work may contain portions from  "ASP.NET MVC" course by Telerik Academy under CC-BY-NC-SA licenseASP.NET MVCCC-BY-NC-SA

Free Software University  Software University Foundation – softuni.orgsoftuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg softuni.bg  Software Facebook  facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity  Software YouTube  youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bgforum.softuni.bg