Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementation of SocketIO in ios

I want to implement SocketIO in my project.

The serverside and client side codes for my project is given below.

At the client side, there is a test button which sends a post request([self.afn post:@"/test" withParams:@{} completion:^(id obj{})]) to the server side. self.afn is my instance object of my wrapper class for AFNetworking library.

The server side catches this post request with the function app.post('test', function(){...} In this post request handler, I emit a data({hello: "world"}) at the newschannel and I expect to catch this data on the client side in the didReceiveMessage handler of the SocketIO library for Objective C.

I checked that, the button successfully sends the post request, and server side successfully handles this post request and emits the data(hello:"world") on the news channel. However, client side does not catch this data on the didReceiveMessage handler.

Where is the problem? Do you have any idea?

The details of the codes for server and client side are given below:

The ServerSide:

//server.js
var express = require('express');
var app = module.exports = express();
/* configure app ... */
var io = require('socket.io').listen(8090);
var mySocket;

io.sockets.on('connection', function (socket) {
    mySocket = socket;
}); 

app.post("/test", function(req, res){
        if(mySocket){
            mySocket.emit('news', { hello: "world"});
        }
});

The ClientSide:

//Client side in ObjectiveC
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.txtFrom.delegate = self;
    self.txtTo.delegate = self;

    //initialize socket
    self.socket = [[SocketIO alloc] initWithDelegate:self];
    [self.socket connectToHost: @"172.20.10.5" onPort:8090];

}

// IBAction for the test button to send a post request to the server to activate
- (IBAction)actTest:(id)sender {
    //self.afn is my wrapper class for AFNetworking
    [self.afn post:@"/test" withParams:@{@"foo": @"bar"} completion:^(id responseObj){
        NSLog(@"Response object: %@", responseObj);
    }];
}

- (void) socketIO:(SocketIO *)socket didReceiveMessage:(SocketIOPacket *)packet
{
    NSLog(@"didReceiveMessage() >>> data: %@", packet.data);
}
@end

EDIT:

I followed the suggestion of Mathieu 'OtaK' Amiot and changed [self.socket connectToHost: @"172.20.10.5" onPort:8090 withParams:nil withNamespace:@"news"];to [self.socket connectToHost: @"172.20.10.5" onPort:8090];

Now, I can see the message at the console, however, when the message is received at the client side, didReceiveMessagehandler does not invoked.

When I'm sending the message from sender, the name of the event is "news", but in the client side I did not mention an event name. Can this be the reason?

like image 503
ankakusu Avatar asked Dec 17 '13 08:12

ankakusu


People also ask

Does Socket.IO work on iOS?

Socket.IO is a framework that makes it easy to implement Socket and the available for iOS, Android, back-end and front-end. In this article you will find some code in Swift (iOS) and Javascript (Web) for implementing the client and NodeJS for implementing the back-end.

How does Socket work in iOS?

The socket communication relies on the client-server logic, where a persistent connection between a server and a client always exists. To be more precise, the server “opens” a dedicated port where clients get connected to it.

What protocol does Socket.IO use?

Socket.IO primarily uses the WebSocket protocol with polling as a fallback option, while providing the same interface.

Does Socket.IO use WebSockets?

Socket.IO is NOT a WebSocket implementation. Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet.


1 Answers

EDIT : The final answer is that you should've used didReceiveEvent delegate method instead of didReceiveMessage

In the mySocket.emit('news', { hello: "world"}); line, you're not specifying any namespace. In your objc code, you're connecting your socket to a namespace :-)

You should be using then : io.of('/news').emit('news', { hello: 'world' });

You were mixing namespaces with event names!

(or just pass nil to namespace in the objc code, it'll be easier)

like image 149
Mathieu Amiot Avatar answered Oct 17 '22 23:10

Mathieu Amiot