iOS WebSocket: Old Fashioned, New Style

From my personal experience as an app developer and staying up to date with the trends of applications that are developing globally right now, I have come to a couple conclusions:

  1. A few years ago, TabBar based applications were very popular. I even saw tab based design incorporated into mobile websites... Isn’t that crazy?
  2. It was not until about a year ago when the slide bar menu started to gain attention for both the clients and the developers.

Top companies such as Flipkart, Amazon, Linkedin, and Facebook, have integrated this kind of design. I have also personally tried it out in one of my applications. Applications have become much bigger than what they previously were; native developers can access more features and iOS’ app store, listing various categories, continues to grow.

Whether you considering mobile or web apps, Think Methodology says that current trends are all about the WOW DashBoard.

In previous years, we were working on web services. After some time, there was a break from web services. For example, if we were creating a dashboard which contains charts, map view, dials, etc., the backend data can be changed continuously. With that scenario, if app can execute frequently request-response, having to change constantly is not a good way to approach the app development. Similarly, Socket Connection is an example that is outdated.

Socket Connection is old methodology. Recently, software engineering students have learned that this socket, enhanced with WebSocketConnection features and iOS, can make things much smoother.

Developing WebSocket in iOS

Integrate WebSocket API for iOS

  1. Install cocoa pods in your mac machine from http://cocoapods.org. (Skip if you have it already)
  2. Go to your project directory, create podfile.
  3. From terminal execute, touch podfile
  4. Write:platform :ios, '5.0'pod 'Socket.io'
  5. Execute pod install and you have WebSocket Library in your project.

Go to ViewController’s .h file

  1. put delegate <SRWebSocketDelegate>
  2. @property (nonatomic, strong) SRWebSocket *webSocket;

Go To ViewController’s .m file

To establish a connection:

- (void)connectWebSocket {

webSocket.delegate = nil;

webSocket = nil;

NSString *urlString = @"ws://20.20.2.68:9090/WebSocket/echo";

   SRWebSocket *newWebSocket = [[SRWebSocket alloc] initWithURL:[NSURL URLWithString:urlString]];

newWebSocket.delegate = self;

[newWebSocket open];

}

Implement Delegate methods of WebSocket

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

webSocket = newWebSocket;

[webSocket send:[NSString stringWithFormat:@"Hello from %@", [UIDevice currentDevice].name]];

}

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

[self connectWebSocket];

}

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

[self connectWebSocket];

}

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

NSString *strResponse = message;

}

Now you are almost done with your web socket connection. Now it is the server’s responsibility to send the data when needed. Just put the json parser stuff at where you receive data.

NSString *strResponse = message;

   NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:[strResponse dataUsingEncoding:NSUTF8StringEncoding] options:0 error:NULL];

Benefits

  • Do not need to request-request continuously.
  • Do not need to involve in timer stuff.
  • Directly get data when you parse it and show to UI.
  • Simply establish connection and open and close as needed.
  • It is fast and furious.

Conclusion

Everything you need to know about outsourcing technology development Access a special Introduction Package with everything you want to know about outsourcing your technology development. How should you evaluate a partner? What components of your solution are suitable to be handed off to a partner?

Let's Talk
Lets Talk

Our Latest Blogs

With Zymr you can