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 news
channel 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, didReceiveMessage
handler 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?
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.
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.
Socket.IO primarily uses the WebSocket protocol with polling as a fallback option, while providing the same interface.
Socket.IO is NOT a WebSocket implementation. Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With