Implementation of SocketIO in ios

10,179

Solution 1

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)

Solution 2

can you try this way

io.sockets.on('connection', function (socket) {

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

}); 

instead of this:

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

app.post("/test", function(req, res){
        if(mySocket){
            mySocket.emit('news', { hello: "world"});
        }
});
Share:
10,179
ankakusu
Author by

ankakusu

Updated on June 08, 2022

Comments

  • ankakusu
    ankakusu almost 2 years

    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?

  • ankakusu
    ankakusu over 10 years
    Thanks for the suggestion, your way is safer but, still, in the client side didreceivemessage does not fire and I'm sure that the message is arrived at the client side, because I saw the message at the console. 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?
  • ankakusu
    ankakusu over 10 years
    Thanks for the suggestion, I edited my post accordingly but, still, in the client side didreceivemessage does not fire and I'm sure that the message is arrived at the client side, because I saw the message at the console. 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?
  • torun
    torun over 10 years
    I had previously developed an application using SocketRocket (as socket.io client) and .net windows service, but I do not have much information about node.js This example: tokbox.com/blog/… may be useful to examine.
  • Mathieu Amiot
    Mathieu Amiot over 10 years
    @ankakusu Maybe you should try to use this delegate instead of didRecieveMessage : - (void) socketIO:(SocketIO *)socket didReceiveEvent:(SocketIOPacket *)packet;
  • ankakusu
    ankakusu over 10 years
    Yes it catches the didReceiveEvent. Can you please edit your answer, then I can accept it as the answer :) By the way, what is the function of didRecieveMessage event?
  • Mathieu Amiot
    Mathieu Amiot over 10 years
    Edited ;) I suppose didRecieveMessage is made for raw websocket messaging as described here : github.com/LearnBoost/socket.io/wiki/Messaging