Using web sockets in your iOS application

Good afternoon, dear readers of Habrahabr!

Today I want to tell you about how to quickly and easily connect web sockets to my iOS application using the chat of the famous cryptocurrency exchange as an example. We realize this with the help of a convenient open SocketRocket solution .

This approach may be useful for:

  • Implementing chat in a mobile client for iOS
  • Using chat information to train neural networks

Interested please ask for a cut!

Add SocketRocket to the project

The github describes several installation options, I will describe the one that I use myself.

  1. We add to our project all the files from the SocketRocket group
  2. Add the following frameworks to the project:
    • libicucore.dylib
    • CFNetwork.framework
    • Security.framework
    • Foundation.framework
  3. Add the HTML character decoder from Google to the project (later you will understand why)

Nothing complicated! Everything is ready to use the full power of web sockets.

Initialize Sockets

Using the methods of information retrieval , deduction, and using responsive exchange developers, I came across an open source, from where I pulled out the right URL for chat sockets:

NSURL *url = [NSURL URLWithString:@"wss://ws.pusherapp.com/app/4e0ebd7a8b66fa3554a4?protocol=6&client=js&version=2.0.0&flash=false"];

We create the socket object itself with the desired request, open it and make ourselves a delegate:

NSURLRequest *request = [NSURLRequest requestWithURL:url];
SRWebSocket *rusSocket = [[SRWebSocket alloc] initWithURLRequest:request];
rusSocket.delegate = self;
[rusSocket open];

Full setupSockets Method Code
- (void)setupSockets
{
    NSURL *url = [NSURL URLWithString:@"wss://ws.pusherapp.com/app/4e0ebd7a8b66fa3554a4?protocol=6&client=js&version=2.0.0&flash=false"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    SRWebSocket *rusSocket = [[SRWebSocket alloc] initWithURLRequest:request];
    rusSocket.delegate = self;
    [rusSocket open];
}


SRWebSocketDelegate Methods

In every self-respecting framework under Objective-C, there must be delegates - and even more so in web sockets. We implement two delegate methods - the first one that is called after the connection is established and the socket is opened, and the second one that is called upon receipt of the message.

- (void)webSocketDidOpen:(SRWebSocket *)webSocket
{
    NSString *helloMsg = @"{\"event\":\"pusher:subscribe\",\"data\":{\"channel\":\"chat_ru\"}}";
    [webSocket send:helloMsg];
}

Everything is extremely simple here: as soon as the sockets open, we subscribe to notifications from the Russian chat. By analogy, you can also write "chat_en".

Next, we describe a method for receiving a message from a web socket:

- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message
{
    message = [[message stringByReplacingOccurrencesOfString:@"///" withString:@""] stringByReplacingOccurrencesOfString:@"\\\\\\" withString:@""];
    message = [message gtm_stringByUnescapingFromHTML];
}

Again, everything is simple. In the first line of the method, we get rid of garbage, in the second we use the category from Google in order to convert HTML characters to readable by the user. Message is our message - further it can be parsed as your heart desires.

Message example
{"Event": "msg", "data": "\" {"uid": "467754", "login": "BTCalexxx", "msg": "anyone in for a short ltc pump to 15?", "Msg_id": 12268748, "date": "04/04/14 07:37:50", "usr_clr": "# 8da0b9"} \ "", "channel": "chat_en"}

Close the connection

Always close the connection when you no longer need it! You can do this using the following method:

[rusSocket close];

Conclusion

Thank you so much for reading to the end! With such a simple method, in our palm we now have the opportunity to have up-to-date information about chat from one of the cryptocurrency exchanges.

In the next article I can describe how to create your WhatsApp using open tools in 4-5 hours of work. Of course, if you, dear readers, will be interested.

Also popular now: