Ya.. it is a good thinking… 🙂
The IM development is depends on the Protocol.
For example :
IF you want to use SIP then SIP is providing Method called “MESSAGE” .
Otherwise It can be other protocols like XMPP.
You can find the XMPP source/package as open source i.e gloox /jabber package. By using these package you can enhance your IM.
Good Luck!!! 🙂