Sending RealTime Notifications Based on Location in NodeJS - javascript

I’m building an app with some location-based requirements. One of the requirements is to send the user a notification whenever the user is at a specific address (think of it like how the Apple Wallet app sends you a notification whenever you get close to, say a Starbucks location).
How would I go about building this kind of functionality? I’m aware that this will require using WebSockets. But, like, how would I do this exactly?
Do I just have the client send his geo coordinates to my backend every time it changes, then compare that to a desired destination saved on a database? Or, is there a better way?
Thanks.

Related

Real-time sockets between Android and server

For learning purposes I’m creating an Android app that does the following:
When a person arrives at school, he/she can check a button in the app and he/she is added in real-time to a list (in the server database) of everyone that also checked “at school” button within the app. He/she can also add a message before clicking the button.
The rest of the students then receive a toast in real-time with the person’s name that arrived and it’s message.
I know how to do the Android part, but what is the best way to do the real-time event queries and requests in communication with the server? For example, I send a socket with the persons confirmation, location and message to the server. From the server-side, I supose there’s a nodejs controller that receives the socket and updates the database in sql. Then it sends a socket to every client online and a confirmation to the checked client.
Is this the “professional” way to handle data like this? What is the best (fastest, more secure, standard) way? When I Google how to make this communication I can only find web server client-server communication with Apache, but I’m really looking for a real-time event with an Android app.
I’m not really looking for code but to know and understand technologies and design patterns on how this can be done. And what to search for in order to learn how to do this.

How do I push mongodb data?

So I have developed a web app as a hobby on Handlebars, Express and Mongoose/MongoDB.
The app let's users create an account and then post advertisements for other users to see and respond to.
All the ads posted by users show up on the index page. So it is common view for all the users on this web app. I am relatively new to web development so to build such a simple app it took me a while but boy I learned a lot!
Now the issue I am facing is, when a user A posts an Ad while the user B is logged in and is currently on the index page (a page that lists all the ads posted) it won't show up for user B unless user B refreshes the page. Rightly so actually because only when the index page's route is hit it will query all the ads and refreshing is basically hitting the index route I get that. But I don't want it that way. I want it to show the new ad on user B's index and pretty much every user's index if there's new ad by any user.
So I did a little research/reading and I learned that I can do it by learning to work with triggers on mongodb and like create some kind of trigger that when a new ad is posted do something. I like the idea but failed to find resources to learn how to use such a thing.
The other option I was suggested was to use socket.io but that too I can't grasp how can I make an entire Ad document work as a socket. I am lost and implementing this feature of dynamically loading ads for all users will complete this hobby project of mine and will help me find a junior dev job in local community.
I request stackoverflow's community to guide me how do I go about doing this and what resources I can use to learn about it.
The socket.io seems to be the best solution for your case. What you will want to do with socket.io is every time a user posts an Ad you use socket.io to notify the rest of the users that there is an update.
If you don't want to send the entire document using the socket you can use the socket to notify the clients and on the client side every time you receive such a notice from the server you will either
a) Refresh the page(not suggested as it will make the user experience unpleasant) which is easier to implement
OR
b) You can use an Ajax request to get the new data from your server and update the fields on the fly(which makes for a better user experience).
Best Way You can come with using Short Polling concept from client side to ask for new data after 1 or 2 seconds (whatsoever count to need ) . Gmail for new inbox mails also uses sync method in a particular fashion . Just ask from server for new data
OR Second option to go through below
On Server Side
Serve index.html page to User A (which is logged in now).Some User B inserts data
Maintain a function or a cron job (checks the count of Total Ads ) Lets say after every 1 minute or so
If there is change in count from the previous total_count , update it and get new mlab documents and send it to function , Let's say push_new_ads which will be sync via socket.io to client
On Client Side
Sync your client_total_count with server_local_count push_new_ads using socket.io and If there is change in count , make a simple fetch api call to get data and appends it to previously fetched array
There is no such way to directly listen the changes in mongodb But you can trigger some changes from oplog using tailable cursors

real time gps reall needs database?

I'm working on a project that involves real time tracking. I'm using html5 geolocation API and I'm stuck where users should locate others. Mine is not storing the lat,lng of the user in a database. Now my question is can I continue my project without database?
Yes you can. When you receive the GPS data, you could directly send it to all recipients. Therefore, you dont have to store it in a database. To be able to create channels with clients that stay open over a longer time, HTTP wont work, you will have to use WebSockets instead. A famous implementatoon for nodejs is https://socket.io

Tracking tweet in realtime with tracking terms changing

I want to build an application that allow users to set a hashtags or mentions that they want to track from their dashboard, and my api will open a web socket to display the tweets in realtime at client side.
If I use twitter streaming api, I need to close and reopen the connection every time user change or add a tracking term. This seems not good, and not scale well.
Is there a better approach for this problem? Please advise.

JavaScript - Button Click to send data remotely

I have a list of company tablets (Android) that are located all around the United States. I would like to be able to select one from a list, compose a message, then be able to send the message to the selected device. Some locations have multiple tablets on the same network, in which i'd like to send to all of them at once if possible.
Is it possible to send data over the internet via JavaScript to an android device, or a list of devices? If so, how can this be done? Is there a certain term for what I am trying to do so I may google this for better results?
I apologize for my lack of knowledge and terms.
There's two options you have:
1. Use websockets to send messages to active devices
In your backend system (you need one) keep a list of active connections. In your webpage, display the connections. Select one, and in an input field type a message. Send this message to the backend, e.g. form post, and send this message to the corresponding connection. In your active device, have an app working that listens to the socket and displays the message.
2. Use Push notifications
You can easily send push notifications to android devices. However, you need a working app on a device before you can send it pushnotifications. Firstly, the app needs to register itself at GooglePlayServices, there's a ton of tutorials on this. Then this will give the app a deviceId, which it needs to send to your backend. In your backend you keep track of all deviceIds in your database.
The same principle holds for alternative 1: display the deviceId's in your listbox, and send it together with a message to your backend.
In the backend send a push notification to the device. There's also a ton of tutorials on how to do that (you need to set up a project in Google to get an API key, which is really easy).
There are probably other alternatives, but these two are the most easy I'm sure.
Also, in order to group send notifications, group devices by their external IP. Make the devices register themselves to your backend so you have their external IP, then group them by that in your database. In the frontend now select an IP instead of a device.
EDIT by FoxDonut (Asker)
I also asked, in the comments below, if it is possible to use push notifications without uploading my application into the Google Play Store. The answer is Yes. Please see this post for clarity.

Categories

Resources