3

I read some tutorials and also checked some examples of Socket Programming for my chat application but I noticed one thing that if we are using socket programming still we need to hit web service again and again in a thread to get new messages from server. Now my question is: If we need to hit web service again and again, then what is the need to do this using socket? We can get chat messages directly from server by hitting web service again and again.

EDIT: I make a chat app simply hitting web service again and again to get new messages, I am using Intent Service. Its working fine but i know this is not the standard way for chatting.

Rahul Sharma
  • 5,949
  • 5
  • 36
  • 46

2 Answers2

5

With sockets you don't need to send new requests over and over again. You create/open a connection between your server and your client. This connection remains open until you close it. Both sides (client and server) can start sending data at any time. WebSockets are a standard for bi-directional real time communication.

There is a nice discussion on the difference between WebSockets and HTTP Requests on StackOverflow: WebSockets protocol vs HTTP

Community
  • 1
  • 1
devz
  • 2,629
  • 2
  • 31
  • 37
1

The point of using websockets over http is having the benefit of real-time. websockets allow server to push the data to the client, rather than waiting for client to connect/subscribe periodically. Also websockets have less overhead than constant http hits.

John Li
  • 43
  • 6