IOS Client connecting to NodeJS socket.io results in Stream end encountered

iosnode.jssocket.iosocketrocket

I have a NodeJS server that uses socket.io and listen to port 8000. The code is something like this:

var io = require("socket.io");
var socket = io.listen(8000);
...
socket.sockets.on("connection", function(client) {
    util.log("Client connects: " + client.id);
});  

I have written a web client that can connects to this server and it works just fine. So the server code is not the problem. The problem is with the iOS client that I am about to describe next.

The iOS client uses SocketRocket and to install it, I use Cocoapods and have a Podfile with the following:

platform :ios
pod 'SocketRocket', '0.2.0'

In MyViewController.h

#import <SocketRocket/SRWebSocket.h>

@interface MyViewController : UIViewController<SRWebSocketDelegate> 
    ...
    @property (strong, nonatomic) SRWebSocket *socket;
@end

In MyViewController.m

@synthesize socket = _socket;
...

- (void)viewDidLoad
{
    _socket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1:8000"]]];
    _socket.delegate = self;

    [_socket open];

    NSLog(@"Connect");
}

- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message {
    NSLog(@"Error didReceiveMessage");
}

- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error {
    NSLog(@"Error connecting websocket");
}

- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean {
    NSLog(@"Error didCloseWithCode %d: %@", code, reason);
}

When the iOS client runs, the log "Connect" shows up. But the delegate didCloseWithCode() also gets invoked. And the log shows

Error didCloseWithCode 0: Stream end encountered

I have spent lots of time trying to figure out, but it's time to get some help from experts. Why am I getting the "Stream end" problem. The NodeJS server is working just fine, since I can connect to it with a web client. What am I doing wrong with the iOS client and how can I correct?

Update: Add JavaScript code on the server to show the util.log() call. When the web client connects, console shows "Client connects"…" message. When the iOS client attempts to connect, nothing on the console.

Best Solution

Turns out that socket.io has its own authentication protocol and is not compatible with SocketRocket out of the box. See this SO answer:

How to use SocketRocket with socket.io?

From the answers there, you could take a look at socket.io-objc

Related Question