Code Monkey home page Code Monkey logo

signalr-objc's Introduction

SignalR-ObjC is a client library for iOS and Mac OS X. It's built on top of two popular open source libraries AFNetworking and SocketRocket. SignalR-ObjC is intended to be used along side ASP.NET SignalR, a new library for ASP.NET developers that makes it incredibly simple to add real-time functionality to your applications. What is "real-time web" functionality? It's the ability to have your server-side code push content to the connected clients as it happens, in real-time.

Installation

Installation with CocoaPods

CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like SignalR-ObjC in your projects. See the "Getting Started" guide for more information. You can install it with the following command:

$ gem install cocoapods

Podfile

To integrate SignalR-ObjC into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

pod 'SignalR-ObjC', '~> 2.0'

Then, run the following command:

$ pod install

Overview

Hubs
SRHubConnection
Core
SRConnection
Transports
SRAutoTransport SRAutoTransport chooses the best supported transport for both client and server. This achieved by falling back to less performant transports.
The default transport fallback is:
1. SRWebSocketTransport (if supported by the server)
2. SRServerSentEventsTransport
3. SRLongPollingTransport
SRWebSocketTransport WebSockets is the only transport that establishes a true persistent, two-way connection between the client and server.
SRServerSentEventsTransport With Server Sent Events, also known as EventSource, it's possible for a server to send new data to a client at any time, by pushing messages to the client. Server Sent Events requires few new connections then Long Polling and therefore will have less latency.
SRLongPollingTransport Long polling does not create a persistent connection, but instead polls the server with a request that stays open until the server responds, at which point the connection closes, and a new connection is requested immediately. This may introduce some latency while the connection resets.

Example Usage

Persistent Connection

using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;

//Server
public class MyConnection : PersistentConnection 
{
    protected override Task OnReceived(IRequest request, string connectionId, string data) 
    {
        // Broadcast data to all clients
        return Connection.Broadcast(data);
    }
}
#import "SignalR.h"

//Client
SRConnection *connection = [SRConnection connectionWithURLString:@"http://localhost/mysite/echo"];

// Register for connection lifecycle events
[connection setStarted:^{
    NSLog(@"Connection Started");
    [connection send:@"hello world"];
}];
[connection setReceived:^(NSString *message) {
    NSLog(@"Connection Recieved Data: %@",message);
}];
[connection setConnectionSlow:^{
    NSLog(@"Connection Slow");
}];
[connection setReconnecting:^{
    NSLog(@"Connection Reconnecting");
}];
[connection setReconnected:^{
    NSLog(@"Connection Reconnected");
}];
[connection setClosed:^{
    NSLog(@"Connection Closed");
}];
[connection setError:^(NSError *error) {
    NSLog(@"Connection Error %@",error);
}];

[connection start];

Hubs

//Server
public class Chat : Hub 
{
    public void Send(string message)
    {
        // Call the addMessage method on all clients            
        Clients.All.addMessage(message);
    }
}
//Client
#import "SignalR.h"

// Connect to the service
SRHubConnection *hubConnection = [SRHubConnection connectionWithURLString:@"http://localhost/mysite"];
// Create a proxy to the chat service
SRHubProxy *chat = [hubConnection createHubProxy:@"chat"];
[chat on:@"addMessage" perform:self selector:@selector(addMessage:)];

// Register for connection lifecycle events
[hubConnection setStarted:^{
    NSLog(@"Connection Started");
    [connection send:@"hello world"];
}];
[hubConnection setReceived:^(NSString *message) {
    NSLog(@"Connection Recieved Data: %@",message);
}];
[hubConnection setConnectionSlow:^{
    NSLog(@"Connection Slow");
}];
[hubConnection setReconnecting:^{
    NSLog(@"Connection Reconnecting");
}];
[hubConnection setReconnected:^{
    NSLog(@"Connection Reconnected");
}];
[hubConnection setClosed:^{
    NSLog(@"Connection Closed");
}];
[hubConnection setError:^(NSError *error) {
    NSLog(@"Connection Error %@",error);
}];
// Start the connection
[hubConnection start];

- (void)addMessage:(NSString *)message {
    // Print the message when it comes in
    NSLog(message);
}

Customizing Query Params

Persistent Connections

id qs = @{
   @"param1": @1,
   @"param2": @"another"
};
SRConnection *connection = [SRConnection connectionWithURLString:@"http://localhost/mysite" queryString:qs];

Hub Connections

id qs = @{
   @"param1": @1,
   @"param2": @"another"
};
SRHubConnection *hubConnection = [SRHubConnection connectionWithURLString:@"http://localhost/mysite" queryString:qs];

Customizing Request Headers

Persistent Connections

id headers = @{
   @"param1": @1,
   @"param2": @"another"
};
SRConnection *connection = [SRConnection connectionWithURLString:@"http://localhost/mysite"];
[connection setHeaders:headers];

//Alternative Usage
SRConnection *connection = [SRConnection connectionWithURLString:@"http://localhost/mysite"];
[connection addValue:@"1" forHTTPHeaderField:@"param1"];
[connection addValue:@"another" forHTTPHeaderField:@"param2"];

Hub Connections

id headers = @{
   @"param1": @1,
   @"param2": @"another"
};
SRHubConnection *hubConnection = [SRHubConnection connectionWithURLString:@"http://localhost/mysite"];
[hubConnection setHeaders:headers];

//Alternative Usage
SRHubConnection *hubConnection = [SRHubConnection connectionWithURLString:@"http://localhost/mysite"];
[hubConnection addValue:@"1" forHTTPHeaderField:@"param1"];
[hubConnection addValue:@"another" forHTTPHeaderField:@"param2"];

Requirements

SignalR-ObjC requires either iOS 7.0 and above, or Mac OS 10.9 (64-bit with modern Cocoa runtime) and above.

ARC

  • SignalR-ObjC requires ARC

Networking

  • SignalR-ObjC uses AFNetworking. The minimum supported version of AFNetworking is 2.x
  • SignalR-ObjC uses SocketRocket. The minimum supported version of SocketRocket is 0.4.x

LICENSE

SignalR-ObjC is available under the MIT license. See the LICENSE file for more info.
SignalR-ObjC uses 3rd-party code which each have specific licenses, see ACKNOWLEDGEMENTS for contributions

signalr-objc's People

Contributors

0xced avatar abillingsley avatar benvium avatar bounin avatar brycekahle avatar cswelin avatar davidfowl avatar echofool avatar joeldart avatar richardgroves avatar tumatauenga avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

signalr-objc's Issues

Consider removing dependency on SBJSON

With iOS 5 NSJSONSerialization is available publicly in both iOS and Mac OS X so there is no real need for SBJSON unless we wanted to support version prior to iOS 5

AFNetworking now supports NextiveJson

To make life really simple for developers who might already be using AFNetworking in their larger project it would be nice to handle json as they would expect it handled so ideally SignalR-ObjC would support all automatic detection of JSON libraries like AFNetworking does (since it is our only required dependency).

Take a look at AFNetworking AFJSONUtilities for specific support.

Update Readme install instructions

The install instructions will not work for mac os x

  • Should not include Reachability or ASI Dialog in mac os x install,
  • Create instructions for using the framework instead of raw files.
  • Be sure to include which frameworks are required for ASIHttpRequest and what flags to set for arc

Create SignalR Errors Correctly

Lets do something like this:

NSMutableDictionary *errorDetails = [NSMutableDictionary dictionary];
[errorDetails setObject:[NSString stringWithFormat:@"%@",errorDescription] forKey: NSLocalizedDescriptionKey];
[errorDetails setObject:[NSString stringWithFormat:@"%@",failureReason] forKey: NSLocalizedFailureReasonErrorKey]
[errorDetails setObject:[NSString stringWithFormat:@"%@",someRecoverySuggestionIfAvailable] forKey: NSLocalizedRecoverySuggestionErrorKey];

NSError *_error = [NSError errorWithDomain:@"com.SignalR.[ClassName].[ErrorName]" code:[SomeCode or 0] userInfo:errorDetails];

Bring client up to date with .Net

Below are the changes made to the .net client that should be added to the objective c client

SignalR.Client Next Release

ConnectionID
SignalR/SignalR@3001fa4

See SignalR Client Sample
SignalR/SignalR@999e05a

SignalR/SignalR@ebedaf1

Additional Transports
SignalR/SignalR@ac3aea3

SignalR/SignalR@cd6b89f#diff-2

Throw if Connection is not initialized
SignalR/SignalR@23efe2b

Store Connection State
SignalR/SignalR@4093a4d

Helper for getting value from dictionary
SignalR/SignalR@f2cc4c9

Only Decrement if queue if > 0
SignalR/SignalR@cea5c80

Remove item from bag
SignalR/SignalR@aeb7955

Add Protocol version
SignalR/SignalR@2038ee2

Fix SL and WP7 Client for missing API
SignalR/SignalR@8eeaecb

Changes from SignalR.Client

Vendor Licensing

Make sure that we are giving proper credit for the vendor project now that there are several

Update clientId to connectionId

This was previously supported but a bug in the SignalR Server project causes

Protocol error: Missing connection id which is an issue available on SignalR/SignalR#41

For now SIgnalR-ObjC will be supporting the release build of SignalR 0.3.5

Protocol error: Missing connection id

Started receiving Protocol error: Missing connection id error after updating to the latest version of signalR.Samples Project and making the switch to connectionId instead of clientId. This seems to be related to

SignalR/SignalR#41 and is probably an error with the server not the client, opening this for now until some resolution is found

ServerSentEvents can take down IIS

When the server has no activity and the client is using server sent events the reconnect process creates new connections and eventually will max out iis's 10 concurrent connection limit on Win7 preventing any further communication without an iisreset

Support iOS 4.3 Target

__weak is not available in iOS < 5.0

The only thing that appears to be preventing SignalR from working on iOS 4.3 is

__weak NSMutableDictionary *hubs_ = _hubs;

We should be able to change this line to

__unsafe_unretained NSMutableDictionary *hubs_ = _hubs;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.