I have a project in objc (using "Socket.IO-Client-Swift" from pod installation) and I'm trying to connect to a socket on URL: https://subdomain.domain.com/word.
My objc code looks:
NSURL* url = [[NSURL alloc] initWithString:@"https://subdomain.domain.net/word"];
_manager = [[SocketManager alloc] initWithSocketURL:url config:@{@"log": @YES, @"compress": @YES}];
_socket = _manager.defaultSocket;
[self.socket on:@"connectedDevice" callback:^(NSArray* data, SocketAckEmitter* ack) {
NSLog(@"socket connected");
}];
[self.socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
NSLog(@"event connect");
}];
[self.socket onAny:^(SocketAnyEvent* e) {
NSLog(@"<< %@", e.event);
}];
[self.socket connect];
Only event triggered is "connect" BUT server (black box for me) should return "connectedDevice" and not the one received since it is set to return this event (connectedDevice)when a new socket is connected. Am I doing something wrong? I cannot find any clue about it.
Logs on console are:
LOG SocketIOClient{/}: Adding handler for event: chrome.connected
LOG SocketIOClient{/}: Adding handler for event: connect
LOG SocketIOClient{/}: Adding handler for event: chrome.joined
LOG SocketIOClient{/}: Handling event: statusChange with data: [connecting]
<< statusChange
LOG SocketIOClient{/}: Joining namespace /
LOG SocketManager: Tried connecting socket when engine isn't open. Connecting
LOG SocketManager: Adding engine
LOG SocketEngine: Starting engine. Server: https://subdomain.domain.net/word
LOG SocketEngine: Handshaking
LOG SocketEnginePolling: Doing polling GET https://subdomain.domain.net/socket.io/?transport=polling&b64=1
LOG SocketEnginePolling: Got polling response
LOG SocketEnginePolling: Got poll message: 97:0{"sid":"Z1pN72vtFg_BFoGeAAHt","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}2:40
LOG SocketEngine: Got message: 0{"sid":"Z1pN72vtFg_BFoGeAAHt","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}
LOG SocketIOClient{/}: Handling event: ping with data: []
LOG SocketEngine: Got message: 40
<< ping
LOG SocketManager: Engine opened Connect
LOG SocketEnginePolling: Doing polling GET https://subdomain.domain.net/socket.io/?transport=polling&b64=1&sid=Z1pN72vtFg_BFoGeAAHt
LOG SocketIOClient{/}: Socket connected
LOG SocketEngine: Writing poll: has data: false
LOG SocketIOClient{/}: Handling event: statusChange with data: [connected]
LOG SocketEnginePolling: Sending poll: as type: 2
<< statusChange
LOG SocketEnginePolling: Created POST string: 1:2
LOG SocketIOClient{/}: Handling event: connect with data: ["/"]
LOG SocketEnginePolling: POSTing
<< connect
LOG SocketEnginePolling: Doing polling POST https://subdomain.domain.net/socket.io/?transport=polling&b64=1&sid=Z1pN72vtFg_BFoGeAAHt
event connect
LOG SocketParser: Parsing 0
LOG SocketParser: Decoded packet as: SocketPacket {type: 0; data: []; id: -1; placeholders: 0; nsp: /}
LOG SocketEnginePolling: Got polling response
LOG SocketEnginePolling: Got poll message: 1:3
LOG SocketEngine: Got message: 3
LOG SocketIOClient{/}: Handling event: pong with data: []
<< pong
LOG SocketEnginePolling: Doing polling GET https://subdomain.domain.net/socket.io/?transport=polling&b64=1&sid=Z1pN72vtFg_BFoGeAAHt
LOG SocketEngineWebSocket: Sending ws: probe as type: 2
LOG SocketEngine: Got message: 3probe
LOG SocketEngine: Received probe response, should upgrade to WebSockets
LOG SocketEngine: Upgrading transport to WebSockets
LOG SocketEnginePolling: Sending poll: as type: 6
LOG SocketEnginePolling: Created POST string: 1:6
LOG SocketEnginePolling: POSTing
LOG SocketIOClient{/}: Handling event: pong with data: []
<< pong
LOG SocketEnginePolling: Got polling response
LOG SocketEnginePolling: Got poll message: 1:6
LOG SocketEngine: Got message: 6
LOG SocketEngine: Switching to WebSockets
LOG SocketEngineWebSocket: Sending ws: as type: 5
LOG SocketEngine: Flushing probe wait
LOG SocketEngine: Writing ws: has data: false
LOG SocketIOClient{/}: Handling event: ping with data: []
<< ping
LOG SocketEngineWebSocket: Sending ws: as type: 2
LOG SocketEngine: Got message: 3
LOG SocketIOClient{/}: Handling event: pong with data: []
<< pong
I also tried to replace initial code with:
NSURL* url = [[NSURL alloc] initWithString:@"https://subdomain.domain.net"];
_manager = [[SocketManager alloc] initWithSocketURL:url config:@{@"log": @YES, @"compress": @YES}];
_socket = [[SocketIOClient alloc] initWithManager:_manager nsp:@"/word"];
but for this case, I'm not getting even event "connect" (that it is unexpected event to be received anyway).
To be sure, I tried to connect to the same URL using Android socket.io library with a simple code like:
mSocket = IO.socket("https://subdomain.domain.com/word");
mSocket.on("connectedDevice", onConnected);
mSocket.connect();
and it worked properly... I've received a "connectedDevice" event as it is expected. Sounds weird but I'm disappointed that it worked on Android and not on iOS... That's why I honestly think that something is wrong on iOS side, but I cannot find what's wrong.
Anyone could point me to what could be happening here?
@jaragones sockets must be created with the SocketManager.socket(forNamespace:) method. So your second attempt would need to use that method. The first method is not supported in this library. Namespaces must be created explicitly.
Awesome! I feel silly now! Thanks for your time, it worked!! :)
Could you tell me how to import 'socket.io-client-swift' lib to objective c project?
@MD04-TanTan, in your pod file you add next lines:
use_frameworks!
pod 'Socket.IO-Client-Swift'
On your controller, you need to add
@import SocketIO;
After running "pod install", it can be that XCode tells you an error that "SocketIO couldn't be found", try to build anyway, it will fix this "missing link" issue.

Thanks for reply @jaragones i tried to build many times but it's not working
@MD04-TanTan You also need to make sure you're using the workspace CocoaPods creates for you.
Thanks u guys !!!! I found it, the problem is missing " use_frameworks! " keyword in my pod file
@MD04-TanTan also notice that you have a comment under your target 'CustomCell' that tells you to uncomment line if you are using Swift
#use_frameworks!