0

I am writing a PHP backend, JS/Jquery front end application that will allow users to "communicate" in near real time. That is the hope at least, my question is what is my best course of action? Am I best to use WebSockets to send data between the server and client or would use AJAX and some sort of timer (x amount of seconds) be better. My worry with the AJAX way is that it may be taxing on the server to have 10+ clients all asking for data every 15-30 seconds. I need it to be near real-time and so 5 minutes is not really realistic.

An example of what I am trying to do would be if I had 5 users all on a page and user 1 updates their status, I would want users 2,3,4, and 5 to see the update as fast as possible without having to refresh.

I am mixed on what I think is best and I don't want to start doing it one way and find out it is insecure or terrible after getting halfway done. What is my best route with an application like this?

AndyPet74
  • 659
  • 2
  • 7
  • 24

2 Answers2

2

Here's a list of popular possible solutions:

  1. Short polling (what you're referring to in AJAX
  2. Long Polling (AJAX too, but not too many requests)
  3. Websockets
  4. WebRTC

So, for short polling as you've said it consumes lots of resources so let's remove that from the list. as for long polling, its idea is that a request is send to server and the server doesn't respond unless a new event has happened (keeps the request) but its rarely used in modern development. so If you're going to work with other developers its kind of bad decision.

for WebRTC, browser compatibility is not that great and still a draft in W3C.

Thus, you're left out with WebSockets, and yes they consume ram but not CPU. ram is much cheaper (and it doesn't consume that much too).

As for security they can be considered equal (except for WebRTC which is better because it is actually P2P Communication)

Side note: don't overthink it :)

Here's some resources that can help you:

  1. https://webrtc.org/
  2. https://github.com/walkor/phpsocket.io //Socket library for PHP similar to Socket.io
  3. https://socket.io
  4. What are the realtime communication protocols available for the web? List of the protocols
  5. https://codeburst.io/polling-vs-sse-vs-websocket-how-to-choose-the-right-one-1859e4e13bd9 great article for polling, websockets & covers SSE too
Mahmoud Atwa
  • 115
  • 1
  • 6
  • Thank you, I am going to read through those. I was worried that polling would kill my server as there could be instances of 30-60 people using it at once. I was thinking sockets but I just wanted to make sure I wasn't missing something glaring as far as security is concerned. – AndyPet74 May 12 '19 at 02:38
  • Glad I could help! – Mahmoud Atwa May 12 '19 at 03:02
-1

there is one way to make the RTC, RealTime App, Just Use the Socket.io "WebSocket " for signaling and before that take a full view of these webPages:

https://bloggeek.me/

https://www.html5rocks.com/en/tutorials/webrtc

https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum

https://www.w3.org/TR/mediacapture-streams/#legacy-interface-extensions

and i start to development this technology with this book enter link description here it will open your view of RTC usage and All Details.

MsDeveloper
  • 17
  • 1
  • 9