简体   繁体   中英

server-side data push for web services

I´ma web programmer and I'm experienced in regular synchronous and asynchronous web services. So the normal way to get data from a web server is to request it (pull it) or to establish a permanent connection.

Now I'm asking myself if there is any possibility to run this the other way round so that the server pushes new data to the client without a previous request of the client by using native technologies like HTML5 and Javascript.

Summarized: I'm searching for a way to realise a server side push system what works with any modern web browser (IE, FF, Chrome, Safari,...) by using regular technologies like HTML5 and Javascript without any plugins or additional software.

The client should just listen for new data as long as the web site is opened and the only connection from the server should happen if new data gets pushed. It must work even through NAT or Firewalls.

Well main reason is to save server load and data transfer with many connected clients. Please also mind that not each push will be a broadcast, single client push must also be available.

Is this somehow possible or still no way to handle it without extra software?

Thank you

Though it's not implemented in all browsers yet, you can try using the standard Web Push API .

You can read more on it in the articles Using the Push API & Push Notifications on the Open Web .

The Push API is a W3C standard to let you enable users of your Web app to get push notifications at any time—even in the background; ie, even when your Web app isn't running in the foreground on the user's device (or even when the browser's not currently running on the user's device).

It uses Service Workers to handle messages sent using common push services, and to allow your Web app to react to the push notifications it receives.

It exposes a new push event to scripts. Here's a simple code example from the Push Notifications on the Open Web article that shows how to use that push event to show an actual notification.

self.addEventListener('push', function(event) {  
  console.log('Received a push message', event);

  var title = 'Yay a message.';  
  var body = 'We have received a push message.';  
  var icon = '/images/icon-192x192.png';  
  var tag = 'simple-push-demo-notification-tag';

  event.waitUntil(  
    self.registration.showNotification(title, {  
      body: body,  
      icon: icon,  
      tag: tag  
    })  
  );  
});

Update 2016-02-12

The Microsoft Edge team recently moved the status of Web Push support in Edge to Roadmap Priority: High — We intend to begin development soon . Since Chrome and Firefox have already shipped support for it, that means once Edge lands that support, you'll be able to send standard push notifications to Edge, Chrome, and Firefox users of your Web apps.

There is WebSockets technology, it allows continuous full-duplex connection stream between a client and a server. More detailed here https://developer.mozilla.org/en-US/docs/WebSockets/Writing_WebSocket_client_applications .

WebSocket is standardized protocol and each server supports it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM