What is the best way to get new content using JSON?

What would be the best way to get new content on a website using json, I got the following system: (looped)

  • client β†’ (I have new content) β†’ server
  • client <- (nope) <- server
  • client β†’ (I have new content) β†’ server
  • client <- (yes, contentId x) <-server
  • client β†’ (get content with id x) β†’ server
  • client <- (Jim Morrison) <-server

Is this the best way? The application will be used for more than 10,000+ users, how does Facebook do it, and still keeps everyone quickly updated?

+6
source share
4 answers

One modern way to do this is to use a server-push system, such as WebSockets or Socket.io, and not a client-push system, which sometimes asks for the status of "there is something new."

With this type of technology, the connection with the client (I assume that it is a web browser) on the server is always open, and whenever something happens on the server, the server pushes data directly to the client.

For code and examples, check out http://socket.io/

Not all server technologies are compatible with this type of approach, but one very good one is Node.js, which allows you to write event-driven server-side code in Javascript. See http://nodejs.org

If you decide to use Node.js on the server and want to interact with browsers, there is even Now.js (see http://nowjs.com/ ) that allows you to actually perform call functions on the client from the server!

+2
source

facebook uses peer-to-peer technology, which means its own cloud of information. Any server can ask any server to provide information or where to go to get this information.

0
source

There are many ways to do this, the most common is polling (which you indicated in your example), in which the client initiates a request and then closes the connection after receiving a response. The second β€œclass” is called HTTP Keepalive , which has a diverse (and ever-growing) implementation list.

The main advantage of the Keepalive HTTP protocol is that after the initial connection, there is no TCP (or SSL) overhead.

Another thing you want to pay attention to is the cluster. This is a huge topic, but I suggest you take a look at these resources to get started.

Web server load balancing:

http://httpd.apache.org/docs/2.2/mod/mod_proxy_balancer.html http://wiki.nginx.org/HttpUpstreamModule

MySQL scaling:

http://dev.mysql.com/doc/refman/5.5/en/replication.html

Please note that there are many ways to scale your site, and I hope others will post alternatives for you.

0
source

I would use browser-based jQuery with this really nice plugin: http://benalman.com/projects/jquery-dotimeout-plugin/

The doTimeout function greatly improves the simple JavaScript setTimeout.

Then on the PHP side there is a polling function that returns 0 if new content is not available. If content is available, continue and return it. It makes no sense to make two trips when you do this.

Just use json_encode on the php side if content is available. Deploy your content in a browser and add as needed.

This is a pretty general overview of how to do this, but looking at jQuery.getJSON documents, you should provide you with all the necessary information: http://api.jquery.com/jQuery.getJSON/

0
source

Source: https://habr.com/ru/post/902348/


All Articles