Reputation: 440
I want to implement a text chat functionality with the help of "Socket Programming" in iOS. I know about the process to connect to a server with the help of host and port like:
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)(@"127.1.0.0"), 80, &readStream, &writeStream);
But I want to know how I can make socket connection between two chat users during chat?
Do I have to use a server between chat users?
Upvotes: 3
Views: 6851
Reputation: 2097
You need a server where the clients can connect to. I would recommed use publish-subscribe pattern.
publish–subscribe is a messaging pattern where senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers, but instead characterize published messages into classes without knowledge of which subscribers, if any, there may be. Similarly, subscribers express interest in one or more classes and only receive messages that are of interest, without knowledge of which publishers, if any, there are. Source: Wikipedia
Subscribe user A’s app to a topic “/topic/user-a”, user B’s app to a topic “/topic/user-b” and publish data to the other user's topic.
For server side, you can use options available like Socket.io, Mosquitto.org, RabbitMQ. Sample Chat SDK Code is iOS: https://github.com/AppLozic/Applozic-iOS-SDK
If you plan to use MQTT, you can use https://github.com/ckrey/MQTT-Client-Framework
Upvotes: 2
Reputation: 7560
You have two options. The one is using a server that two clients connect to. If you don't want that, you will have to implement TCP servers into the clients.
So aou defenitively need any kind of server, the clients can connect to. It's not pretty complicated to implement a server using CFNetwork. Just google for 'iOS TCP Server' or similar.
I'm not at the office today, so I unfortunately can't post code where you can start with, I'm sorry.
When I started TCP programming I found this tutorial pretty useful. Ray Wenderlich makes good tutorials though.
If you don't get it working, I will post some code on monday when I'm back at my code base :)
http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server
Upvotes: 4