Erika Electra
Erika Electra

Reputation: 1882

What Websocket library for a Node.js server works best with iOS clients?

On the iOS clients, I'm using SocketRocket by Square: https://github.com/square/SocketRocket

Everywhere I have looked, I have found comparisons of Websocket libraries based on web applications accessed from browser, or queried in a database, but nothing as yet for clients that are iOS smartphone apps.

The clients would connect to the remote server on request through the app (i.e. the connection isn't "always-on" or done through a mobile browser or proxy or GameCenter), and, once connected, be paired with other clients in a two-player "game" situation. Until a match ends, the connection would need to persist, and the server would be responsible for timing each user's turn and receiving & issuing commands from/to each user, sort of like a turn-based game except each turn has a server-managed time limit. After a match ends (generally 15-20 minutes), if a user doesn't want another match with another random opponent, then the connection would be closed and the user logged off; users that want to continue would then be matched with another user by the hosting server (running Node.js and the Websocket library).

Some of the options I have considered include Socket.IO 1.0: http://socket.io/ Sockjs: https://github.com/sockjs ws: https://github.com/einaros/ws nodejs-websocket: https://www.npmjs.com/package/nodejs-websocket

but heard from https://medium.com/@denizozger/finding-the-right-node-js-websocket-implementation-b63bfca0539 that Socket.IO isn't optimal for heavy user traffic (and I'm anticipating more than 300 users requesting matches at any one point), and that Sockjs doesn't have some command query feature, but didn't quite find a conclusive answer in the context of smartphones or iOS devices -- not browsers -- either way, in any situation.

The question is what Node.js server Websocket library might play nicest or interface with the fewest stability/scalability/complexity concerns with the iOS clients running SocketRocket? The SocketRocket wiki itself isn't helpful as it uses a Python/Go-based server side test.

EDIT: Potentially helpful resource: http://www.teehanlax.com/blog/how-to-socket-io-swift/

Only missing thing is a comparison or discussion of other potential websocket APIs, not just Socket.IO. But this is a start in that it seems to be working with the latest iOS, SocketRocket, and Socket.IO builds.

Upvotes: 0

Views: 1325

Answers (1)

Halordain
Halordain

Reputation: 129

I like Sockjs because it is simple. Here is an implementation for SocketRocket --> Sockjs that works as proof of concept

NEED: -SocketRocket (add libicucore.dylib, Security.framework and CFNetwork.framework to your project) -Node.js -Sockjs Server

SERVER:

var http = require('http'),
    sockjs = require('sockjs'),
    sockserver = sockjs.createServer(),
    connections = [];

sockserver.on('connection', function(conn) {
  console.log('Connected');
  connections.push(conn);
  conn.on('data', function(message) {
    console.log('Message: ' + message);
    // send the message to all clients
    for (var i=0; i < connections.length; ++i) {
      connections[i].write(message);
    }
    //
  });
  conn.on('close', function() {
    connections.splice(connections.indexOf(conn), 1); // remove the connection
    console.log('Disconnected');
  });
});

var server = http.createServer();
sockserver.installHandlers(server, {prefix:'/sockserver'});
server.listen(3000, '0.0.0.0'); // http://localhost:3000/sockserver/websocket

CLIENT (ViewController.m):

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    SRWebSocket *myWebSocket;

    __weak IBOutlet UILabel *connectionStatus;
    __weak IBOutlet UITextView *myTextView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    connectionStatus.textColor = [UIColor redColor];
    myWebSocket = [[SRWebSocket alloc] initWithURL:[[NSURL alloc] initWithString:@"http://localhost:3000/sockserver/websocket"]];
    myWebSocket.delegate = self;
    [myWebSocket open];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message{

    myTextView.text = message;
    NSLog(@"message: %@",message);
}

- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean{

    connectionStatus.text = @"Disconnected";
    connectionStatus.textColor = [UIColor redColor];
}

- (void)webSocketDidOpen:(SRWebSocket *)webSocket{

    connectionStatus.text = @"Connected";
    connectionStatus.textColor = [UIColor greenColor];
}


- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error{

}

@end

src: http://nunoferro.pt/?p=22

Upvotes: 1

Related Questions