Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For a push notification, is a websocket mandatory?

I have PHP on the server side, and HTML and javascript on the client side.

I am making an app where a stakeholder types a message that is broadcasted to multiple recievers of a group in real time.

I did some research on google and I understand I need to use WebSockets or Comet for real time push notifications. Is WebSocket or Comet mandatory for sending mass notifications to users?

Is my understanding correct? Any references to start with?

like image 827
John Franky Avatar asked Jun 24 '15 19:06

John Franky


People also ask

Does push notification use WebSocket?

Push notification is are simple messages from apps installed on a device that wake up the handset and alert the user with a message displayed on the home or lock screen. WebSocket is a protocol providing full-duplex communication channels over a single TCP connection.

What is needed for push notifications?

A push notification is a message that pops up on a mobile device, such as a sports score, an invitation to a flash sale or a coupon for downloading. App publishers can send them at any time, since users don't have to be in the app or using their devices to receive them.

Why WebSocket is required?

WebSockets work by initiating continuous, full-duplex communication between a client and server. This reduces unnecessary network traffic, as data can immediately travel both ways through a single open connection. This provides speed and real-time capability on the web.

What can I use instead of a WebSocket?

SSE is an excellent alternative to WebSockets. They are limited to the browser's connection pool limit of ~6 concurrent HTTP connections per server, but they provide a standard way of pushing data from the server to the clients over HTTP, which load balancers and proxies understand out-of-the-box.


2 Answers

If the client is a browser, then the ONLY two ways a standard browser can connect to a server is via an Ajax (e.g. http) request or a webSocket connection. So, if you want a client to get notified of something from the outside world it has to use one of those two mechanisms.

HTTP requests are transitory. The client makes a request of a server, the server responds. HTTP requests are perfect for the client requesting information from the server. They are not very good at the server sending information to the client because normally the client is not connected. There are hacks and work-arounds where the client "polls" the server on some interval and maybe even the server uses longer running requests to try to simulate a "push" type system, but they are sub-optimal hacks at best.

webSockets are continuous connections. The client connects and the connection remains in place for as long as both sides want. This allows either side the ability to send a message to the other side whenever they want. That means the server can "push" data to the client whenever it wants. webSockets are efficient for push connections and are recommended (this is one of the main things they were designed for).

Comet is a library that was originally built for using HTTP to try to "hack" or "simulate" push before webSockets were invented and then before they were widely supported. I can think of no reason why one would want to use Comet instead of a webSocket unless you had such an old browser that webSocket was not supported.

So, if you are trying to do "realtime server push" to a browser, then you must have a continuously connected socket from the client which means webSocket (or something built on top of webSocket like socket.io).

For phone apps where you have access to the phone SDK, you can use the "push" system built into the OS to push some messages from server to client. This isn't quite the same as the two way webSocket channel, but since you asked about "push notifications", the OS push services available in both Android and IOS could also be an option for pushing notifications from server to client. Here's info on iOS notifications and Google Cloud Messaging

As of 2016, one can also use Server-sent events in all modern browsers except Microsoft browsers (not supported yet in Edge or IE) to push data from server to client. Here's a browser compatibility table. Server-sent events use a long lasting HTTP connection, a special MIME type and a supporting client in order to be able to send events from server to client at any time. Unlike webSockets, server-sent events are one way only (from server to client). A client would then use a traditional Ajax call in order to be able to send data to a server (whereas with a webSocket data can be sent either way over the same webSocket connection).

Here's a good description of how server-sent events work: How do server-sent events actually work?

like image 82
jfriend00 Avatar answered Sep 19 '22 15:09

jfriend00


Is your client application a SPA? (Single Page application)? It's very important because if not, you have to consider that everytime a client change page, connection with websocket server will be lost. In this case you have to manage a queue because if stakeholder send a multicast request when one client is disconnected, client won't receive nothing. Polling won't solve this situation too and it's an orrible solution because mobile clients (for example) with typical internet plan, will consume megabytes for unuseful "ping" traffic. A real example of polling is a child in a car asking his dad every minute if they are arrived to a destination! So, Is there a solution without using spa? Yes, using a "shared storage" between stakeholder and clients, and using websocket only for "wake up" online clients saying: Hey there is something new, go to check!

Everytime a client open a page it will receive from backend also not-read notifications, taken from the storage. When a stakeholder want to notify something, it will just store the notification message in the shared storage and send a "pulse" to notification server. Notification server will forward the "pulse" to online clients (just in case someone is stuck reading a page). If a "pulse" is lost because a client is changing page there is no problem because the client will bring notifications from the storage. Every page will contain this logic: Retrive number or unread notifications (server side) Connect to the notification server after 5 seconds (javascript side). Hope it helps.

like image 41
Marcello Kad Avatar answered Sep 21 '22 15:09

Marcello Kad