How to create a service worker with persistence connection - javascript

I want to create a service worker which have a persistence connection with the server. I can't use GCM or any other third party to do that. i want to implement the server by my own.
When there is an update, Server should push a message and Service Worker should receive it, and display it as a notification. Even there is no relevant tab open it should display the message.
(I tried Eventsource and it is not suit for my requirement.)
And i want a solution in java.
can anybody give me a solution ?

In my mind it is not possible.
The browsers are implemented to trigger push event only when they receive a message from GCM for Chrome and the own Mozilla push system for Firefox.
Also you can't use websocket with service-worker.
Maybe you'll be able to do something with the Periodic synchronization when this feature will be available, but it's not really its purpose.

Related

Can Firebase Messaging Service be a scalable replacement to websockets / server sent events?

I am developing a webapp and currently my app server uses Server Sent Events to maintain a connection to the user to efficiently push things like new messages and other events to them without them having to constantly poll the server to ask.
I'm looking for a way to implement Push Notifications such that I can use the Notification Web API to deliver notifications to the user either when they are in a separate tab, or when they have it running on their phone and they have Chrome minimized.
After some research, it looks like Google Cloud Messaging has been replaced by Firebase Cloud Messaging, and this is the recommended service to use to deliver push notifications. It looks like it works by having the user maintain a persistent connection the Firebase Servers and your personal app server posts requests to Firebase for them to deliver to the user.
My question: does this alleviate the need to implement and maintain an SSE / WebSocket server? I'm wondering if I can't just forward all my events through FireBase instead and have them be delivered to the user via their service. That is, I would have two classes of messages both sent from Firebase:
One would be the typical "notifications" which are interpreted as such by the user (such as a new message), and require local permissions for the Notifications API. And then the other type would be other "real-time" updates that wouldn't require a notification (such as a message being edited, or the "user is typing a message" prompt)
Is this sort of thing possible / recommended, or is my understanding flawed in some aspect of this?
Nothing you're describing seems outside of capabilities of FCM.
It looks like it works by having the user maintain a persistent connection the Firebase Servers
Actually, it's not the "user". It's the messaging infrastructure provided by the device. For Android, the message get routed through Google Play services, which runs as a privileged process in the background. For iOS, messges go through Apple's APNS. These components maintain an open socket to their respective services, and they're more effective than the app managing the socket on its own, because the app can't manage a socket in the background indefinitely - the OS will shut it down after some time. This means the app can wake up and receive messages soon after they're sent.

What is the differnce between Service Worker and GCM

I am really confused about the two technologies GCM and Service worker. We can use both for push notifications but the service worker are specifically for browser push notifications and GCM for mobiles. This is my theory about the above mentioned technologies. If i wrong then please share your valuable suggestions and give me a right guide.
Service Worker
A service worker is basically a special javascript environment, that runs in the background of browsers. Websites can register service workers and then they can run some code in the background.
GCM
Is a product of Google, which basically provides a server API for sending messages across devices. If you combine them with a service worker, you can send notifications to Chrome whenever you want (if the user allows you to register a worker).

How to listen to changes in background (Parse Server)

The task I want to accomplish is the following: Change some fields in the Parse dashboard and make the app respond right after. So pretty much make the UI respond to a change in the server.
After searching for a while, I see that I should set it up through Parse Cloud Code and use methods like afterSave. But as of now, I understand how to listen to an object being saved. How should I manage to do it the opposite way, which is the app listen to a change in the server? I will be manually changing some variables in the Parse Dashboard and I want the user to be able to get it whenever I manually change it. I could set up a timer (as a method inside the app’s code) and check it from the server in a 5 seconds interval but that would be pretty much inefficient in terms of requests, internet data.
You have few solution choice.
I dont know your architecture and your technologies what you use but I wrote two solutions.
silent notification with push server:
You need a push server on backend and you can send a silent push to frontend when the afterSave code triggering. However frontend code should be subscribe your push server.
e.g.: https://justmarkup.com/log/2017/02/implementing-push-notifications/
websocket communication:
You need a socket server on backend and a socket client on frontend and if you have a channel then you can send a message to client from server.
e.g.: https://blog.idrsolutions.com/2013/12/websockets-an-introduction/
I hope It will be help you.

Need to call data from insecure URL for GCM push notification for chrome

I need to call an insecure (http://) URL to fetch data form service worker file for GCM push notification for chrome. Is this possible anyway?
I did a long search and as #Jeff Posnick written in my another question's(how to register a service worker from different sub domain) answer. It seems for security reason it's not allowed by GCM team to call a insecure service in service worker or while working on GCM push notification for chrome.
Not to reboot this thread, but I stumbled upon this as well. Two solutions are creating a github repository (where you have a standard ssl-certificate) or getting a free certificate from startssl. For the last one, you might need permissions that are not standard in your hosting package.

How exactly does Server-Sent Events work?

I am trying to get into the web push technology so I started looking around. I have basically found 2 technologies, that is Websockets and SSE. After ruling out Websockets because of lack of perl support, I wanted to try out the more native SSE-approach.
Now, trying to get SSE to work is a real pain in the arse. Every documentation has conflicting information and there does not seem to be a general consensus on how SSE works. Some say hat you need an <event-listen src="events.pm"> tag, others say you only need an EventSource JS object. Even with the EventSource object, I found around 4 possible implementations and none of them seem to work.
Here is what I have. I have an events.pm, which uses mod-perl. If you call that file, it returns data: I haz a websocket. That is sent with the content-type application/x-dom-event-stream.
The HTML and JS files have been rewritten so often with different implementations that I have given up on them. Can you guys please give me a working example?
Also: I do not understand how you can send specific messages to the client. Sending a predefined message seems to be fine. However, if I imagine a situation where someone sends me a message, I do not understand how exactly that information ('there is a new message for you') is transmitted to that exact browser that needs that information. Every post I found on this is vague at best.
EDIT
Basically, what I need is a way to say 'hey, are you allowed to get this notification? show me your id/session/token first!' on a per connected client basis. I wonder if it is at all possible with SSE.
I would say that Server-sent events are described pretty good in Stream Updates with Server-Sent Events article, namely the Event Stream Format. With Firefox 6 SSEs are now supported by majority of modern browsers (unfortunately except IE).
I do not understand how you can send specific messages to the client.
Sending a predefined message seems to be fine. However, if I imagine a
situation where someone sends me a message, I do not understand how
exactly that information ('there is a new message for you') is
transmitted to that exact browser that needs that information.
Clients are connected with the SSE server by keep-alive event stream, uni-directional connection which isn't closed until browser/tab is closed, user invokes close method or network connection is lost.
Basically, what I need is a way to say 'hey, are you allowed to get
this notification? show me your id/session/token first!' on a per
connected client basis. I wonder if it is at all possible with SSE.
This kind of authentication logic would be required to do on the server side, so it's dependent on the language or platform you are using. Unless there is library for that in your environment you would probably have to write it.
The spec has changed/evolved. Current version uses EventSource object. The HTML element is gone.
The current MIME type is text/event-stream. The x-dom-event-stream was used only in early implementations (Opera in 2006).
To identify/authenticate clients you can either use cookies or connect to an event stream URL that includes some kind of authentication token (session ID), and then you'll know that a particular TCP/IP connection is associated with that user.
http://html5doctor.com/server-sent-events/
Adding to #porneL's answer; you can tell EventSource to include cookies to the request it sends. Just create the EventSource object like this:
var evtsrc = new EventSource('./url_of/event_stream/',{withCredentials:true});
I tested this with latest version of chrome and firefox.
Source: http://www.sitepoint.com/server-sent-events/
edit: apparently this is not necessary / useless. See #Tamlyn's comment.

Categories

Resources