Simple PHP + NodeJS approach for push notifications - php

Most of my website is PHP, and I want to start making a little better UI with live push notifications. I read a few articles and posts here as well, but non provide concrete examples. It's really hard to learn by yourself especially when Node still has no tutorials in abundance, mostly beginner's tutorials.
Let's say I have a PHP page: post.php which allow user to enter a post, and I use PDO to insert that post to the MySQL db.
What is the simple way to connect a node.js (on a different port) and listen to that PHP page, to then in turn show a notification on the navbar, for example?
I know I will need to use socket.io as well.

Related

Back-End database for image based TableView

I am some what a beginner in iOS development, and am beginning to move from the front-end dev to the back-end. I know using JSON with php and MySQL is probably the most common to use for loading table views from databases, but I am wondering if anyone has some insight in where and how to start this process for building a database to load image based user-generated content to TableViews (much like instagram).
I know this is not a programming specific question, just trying to get some direction on where to pin-point my efforts. Appreciate any and all input.
Okay so learning how to build a database(MySQL) . I found some links that can help you.
http://www.wikihow.com/Create-a-Database-in-MySQL
http://dev.mysql.com/doc/refman/5.1/en/database-use.html
It isn't advisable to store images in tables because you'll encounter problems when it comes to backing up. Save images in a folder on the server and store their references in the tables.
To Access this images from your app. You'll need to make a post request to your database through an API written in PHP. You can find a good tutorial on how to write a simple PHP API script on google.
I don't have enough reputation so I wasn't allowed to post more than 2 links. So my answer continues
From your app you hit your API with the necessary parameters you set when writing the PHP script with a POST request. You API should return you the url of the image you seek and you should be able to download the image in your app.
This is a tutorial on how to make a simple NSURL Connection
http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/
Below is another link that might be of good help. Its a simple tutorial that builds a messaging app that hits an API and gets messages from a database. There is a lot of learn from there to fulfill your need.
http://www.ibm.com/developerworks/library/x-ioschat/
As an alternative to the traditional ways I find Parse very useful. Parse can manage your whole backend and they have a very powerful free basic plan.
If you focus on images dive here in, to decide wether Parse fits for you or not: https://parse.com/tutorials/saving-images

How does Facebook notify and instantly shows new comments or how does Stackoverflow do it?

I am a PHP developer and the title basically says it all. However I was hoping on some more in-depth information as I am starting to get confused about how the flow for the project I work on should go.
For an (web) application I need to implement a feature like Facebook does it with notifying users about replies/comments and instantly showing these.
I figured I could use long-polling with ajax requests but this does not seem to be a nice solution as the notifications never really are instant and it is resource heavy.
So I should use some form of sockets if I understand correctly, and Node.Js would be a good choice. So based on the last assumption I now get confused about the work flow.
I thought about two possible solutions:
1) It seems to me, that if I would use Node.Js I could skip using PHP at all and base the application on Node.js only.
2) Or I could use PHP as a base and only use Node.js for notifying users and instantly showing messages but saving the data using PHP and Mysql.
These two possibilities confuse me and I can't make up my mind about what would be the "best" and cleanest way.
I do not have much experience in Node.js, played with it for a while. But managing and saving data seems to be hard in Node.js so that is why I came up with option 2.
I know Facebook is build on PHP so I am assuming that they save the data via PHP and notify / instantly show replies and comments via Node.
Could someone help me out on this?
Thanks in advance!
EDIT:
I just noticed, Stackoverflow does something similar. I get a notification in the upper left, and below my question a box with "new answer to this question". I am really interested in the technologie(s) used.
Well you could use node.js for the notifications and PHP for your app.
By googling I found this about real-time-notifications.
You could also just use node.js with socket.io, but this means that you have to learn new technologies as you mention that you have no experience with node.
I haven't used it but you could check this project, for websockets in PHP.
When you have an update that you want to notify users you can use the publish subscriber pattern to notify the intrested in this update.
Take a look in Gearman too.
Personally, I've built a notification system using the pubsub mechanism of redis, with node.js+socket.io. Everytime that there is an update on a record then there is a publish on the appropriate channel. If the channel has listeners then they will be notified. I also store the last 20 notifications in a Redis list.
The appplication is built in PHP. The notification system is built in node.js. They are different applications that see the same data. The communication occurs via redis. For example in the Facebook context:
1) A user updates his status.
2) PHP stores this to the database and Redis
3) Redis knows that this update must publish to the status channel of the specific user and it does.
4) All the friends of the specific user are listening to his status channel (here comes node.js)
5) Node.js pushes the notification in the browser with socket.io
As for facebook, I have read in an article that is using long polling for supporting older browsers. Not sure for this though, needs citation...
AFAIK It would be via two simple methods :
First one that could be very simple is adding a Boolean column to each record that determines if it has been notified or not.
The second method is creating a table to insert all notifications.
However, I'm not sure if there are alternative methods for better performance, But first method is what I do commonly myself. But I think Facebook is using 2nd method, because it has to notify each one to a lot of users.
Your question maybe dublicate of:
Facebook like notifications tracking (DB Design)
Database design to store notifications to users
You could use Server Side Events it involves a bit of JavaScript but nothing overly complicated I think.
The main bulk of this method is PHP though, so you would just use the PHP to query your DB for notifications and SSE will push them to the user.
It does have some limitations though, most notably it's not supported by IE (huge surprise) thought i'd mention it anyway to let you know of other possibilities.
Hope this helps

Setting up an online server database and code my android native app to interact with it

Im developing a very simple native Android App containing a simple Main Menu with the Today, Yesterday, This Week, This Month and Search by Date options.
(I bought the lynda course: "Building Android and iOS Apps with Dreamweaver" with HTML5 and with the design and functionality of the app im not having too much trouble.)
What i entend with this app is this: i will write famous people profiles every day (between 1 and 5). So when people click (for example) in the Today option they will access to the profiles list that i have created for today (Yesterday and others is a check on the past written profiles from database).
So imagine that some android user clicked on today and will be presented with 3 profiles options ("Brad Pitt", "Shakira" and "Cristiano Ronaldo"), that i need the app to access my server database to update it (the "Today" profiles).
and finally...
Before the questions i want to say that i have searched on stackoverflow and found very similar answers, but im a novice on programming and when i'm reading the answers they can differ so much that i got confused going link on link documentation.
The Questions:
I want to create somekind of method that i could have a template with different profiles (Singer, Politician, Soccer Player) and click on somekind of button to start a new profile/article with Born Date, Occupation, Nationality, Biography and etc. Its this possible? How can i do it and in wich programming language?
What kind of server and what kind of database do i have to create and how to configure it to send the info to my app?
There is any server specifications that i must have for this to work the smoother and easiest possible? For this issue price is not a problem.
What kind of code should i write on my app so that it could get the "3 profiles update for today" from my website?
Its possible to show me some code example that would work with this simple step in my app of selectiong "Today" button on main menu and the app check for new profiles for today and present the user as an option? Because if i had this bit of code i could understand better how to do the rest with the profile details on the "Brad Pitt" option for example.
Hope you understood my questions. Thanks in advance. Have a nice day
Assuming that you're using the Phonegap Framework to develop your application, I would do it like so:
First of all get a Server/Webspace capable of running PHP Scripts and a MySQL Database.
You also have to rethink the way these applications work, for example you can't
configure it to send the info to my app
You need a Server Side Script (i would choose PHP for this, it's easy to learn and available on almost every Server). This PHP Script then queries your MySQL Database where all the information about the people is stored. On thing to remember is that your Clients will poll the server and request the content, you're not actively sending anything to Clients unless requested, that's just the way how HTTP works. The interface your Script is providing there is called a REST Interface.
Have a look at the link, it explains everything pretty good. Your output format should be JSON this makes it really nice to work with in the Client App later on. To do things like "what was posted today?" Have a look at how SQL Queries work and implement it in your Script.
Now for the client you can access this data by using JavaScript, most likely jQuery to simplify everything a bit. Use the jQuery $.getJSON function to access the data provided by your REST Interface. Now you can use basic jQuery to wrap this data up with HTML to present it to the user. I know that this is not "example code" or anything you requested, but it should point you into the right direction on how to achieve something like you want to do. Just do a google search for all the keywords listed above and keep reading about it, then you should be able to build a app like that.

RealTime live notification Jquery PHP

I'm tried to implementing a live notification which is like the facebook newsticker and the notifications(on top). I was wondering what's the good solution to archieve this. I implemented an chat application before which I fired AJAX GET request in certain time to archieve the realtime, but it seems not good. I checked facebook using firebug, there is no GET request fired(or it is hided?)
Now here is my scenarion:
In main.php
I have a live ticker and a notifications button.
In bulletion.php and User.php
When I perform an add user,it will go heading to the bulletin or user.php.
How can I get informed in main.php when bulletin.php,user.php successfully created in database?
I checked this question as well before I asked.
notification system in PHP/jQuery
Realtime and php?
I was wonder,what's is long poling Ajax and session-based notification.How it can be archieved? I know Node.js can be good in implementing realtime, can it combine with PHP? and memcached ?
Any can provide sources to refer or example might be good.Thank you.
As a side note, PHP isn't really the best language to use when it comes to push notifications, it's really built around typical get/response kinda flows.
I use PHP for all my page stuff, but when it comes to push notifications of any sort I really like http://www.nodejs.org/ and http://socket.io/ to go with it. They're very easy to get setup, and will play well with you using php for the majority of your work, then using node to deal with push notification kinda stuff.
Have you thought about using HTML5 WebSockets? Have a look at EventSockets and the kickstart project on Github.
I've had much success with the server-sent events standard. It's very simple and works perfectly but it's only supported in modern browsers.
Meteor is a comet server using PHP and JavaScript to push data to browsers. It's very slick and worth a look, although it may be a little hard to setup/implement.
Edit: Quick demo here

PHP:Web Sockets and html 5

I am working on a project where i am trying to build an interface like If user is logged in and in between that if sever has any message for that user then the user will get notify immediately (Like facebook ) so for i am going through websockets but confused from where should i start ? please suggest me some basic ideas ?
I am using PHP as a language for implementation
I'm looking for advice at the moment on achieving real-time notifications using a mix of PHP, NodeJS and socket.io.
At the moment I have done next to nothing with Node and socket.io so I'm at a loss with the best way to achieve this. I've done some basically listening and what not but nothing extreme.
For now my questions are:
How will socket.io know of new notifications, should PHP be responsible for sending the data (say a post was created) to socket.io and from there broadcasting to connected clients?
How does socket.io know which clients want the notifications?
I'm really not sure what else. This is all a bit much for me at the moment. I just can't get my head around how the notifications will be distributed.
Any advice would be greatly appreciated. I don't expect a detailed walk through, just a push in the right direction or some knowledge on how it all meshes.
Start from reading what is websockets, how they work, what browser support. The internet is full for this information.
For php implementation, take a look at the http://socketo.me/. PHP is not best suited for socket communication. I would use nodejs+socket.io and php for 'regular stuff'. Don't be afraid to mix technologies, there is no hammer for all problems.
Edit:
If you don't know nodejs then it could be hard, but it's worth your time to learn it.
Some ideas:
You can create table with notifications on your database, then if some event acccurs, ex. post added add record there (like number of new posts, and user_id), then from nodejs side connect to database fetch database and look for notifications for current user.
Then nodejs notifies listening clients about changes (socket.io)
If user reviews new posts update counter to 0, (something similar what facebook does when you have new message), and then nodejs will know that there are no new post, so nothing to notify.
The hardest part is to get current user session.
Some related:
http://socket.io/
What MySQL drivers are available for node.js?
Sharing data between php and node.js via cookie securely
Authenticate user for socket.io/nodejs
Socket IO send to specified user ID

Categories