implementing chat system php, obj-c, and mysql - php

I am a newbie API developer using PHP and we have a new client who wants to include chat system in the app that he wants to develop. I already created the native way by creating a table in mysql with sender, receiver, message, time_stamp field and I already created a set and get API call for Messages. But the client seems not satisfied because by default it is not real time. My front-end developer just call the GetMessage() on an X seconds.
What I want is to make it real time just like what Facebook or Skype app do. When new messages was inserted in the database the server will just poke the app that there is a new message via push notification I think? So in that case the app will not get messages on every X seconds. So basically once I hit the send button, on the other side, the receiver will just see it synchronously.

Take a look at something called triggers. They are activated in mysql when insert, update or delete-event occurs. An important thing though is that SQL has to used for triggers to be executed. The triggers would not execute from external api's.
You might for an example set some value in a table that tells that a new message has arrived for a certain user when a new insert is made into the db.
Starting points:
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
http://dev.mysql.com/doc/refman/5.0/en/faqs-triggers.html

Related

sending automatic push notification from mysql to android mobile

I have a database that contains some data and i want to get notified automatically on my mobile app if any value in the database is changed without having to check the database every certain time,i have tried many methods and viewed several tutorials but it leads me no where,any help ??
This is not going to be possible with only database. You need to write down an RESTful API which your app needs to poll to look for changes.
Ideal way of doing it is using GSM to push notify your app when ever you change something in your app. This will again require PHP/ASP/NodeJS etc... There must be some way you are updating your database? This should go exactly there.

Determine if user is online otherwise, otherwise close chatroom (php)

I'm making a website which is going to have different chatrooms. Any user can create a chatroom at any time, and another user may join a chatroom when it is available. Max two users can chat in a single chatroom at the same time, but multiple chatrooms can exist.
I'm using AngularJS and PHP, with the PubNub API for the chat functionality. The created chatroom will be stored in a MySQL database, with the following fields:
User1: the user who created the chatroom. It may change or be null if it leaves.
User2: it will be null at first place and will store the user's name of the person who joins the chatroom.
Closed: when both users are offline, it will be true (1), then nobody can join anymore.
I have to update the columns "user1" or "user2" when any of the users leaves the chatroom. Then, check if both users are offline and then update the closed value.
I know that I can save the user's last connection by calling a PHP function via AJAX every 60 seconds, for example. Even I could check if the other user is still online by checking his last connection in the same function, but who is going to call the function to check if the last user left?
I wonder if I have to do that verification every time either any users request the available chatlists or I have to resolve it with another approach.
I guess that I can set a timeout function in PHP each time a user joins/creates a chatroom. That function is going to update the user column to null and update the closed value if both are null. When the user is in the chatroom, each 60 seconds another function will be called to postpone the first one. But I don't know if it is possible, and if it is possible using shared hosting.
I hope you can help me and thank you very much for your attention.
PubNub Presence: Realtime vs Polling
The answer is - do not poll for presence, instead, have your server listen for changes in channel presence using PubNub Presence Web Hooks. Please read this article thoroughly as it details all the aspects of PubNub's Presence Web Hooks and then review the official documentation for PubNub Presence Web Hooks. The sample code for implementing the REST endpoint on your server to receive the web hooks is Node but you can use that code to implement that in PHP if required but if you can use Node for this purpose, it might be a better choice (and you could still use PHP for everything else).
I know your server has a chat room creation process that inserts new chat room record in your database so you don't need to know when the channel becomes active, but when user1 subscribes to the chat room channel, PubNub will send a channel active event via a web hook to your server, if you need to know when that happens.
When user2 subscribes to user1's chat room channel, PubNub will send a join event to your server via web hook and your server can use that event to update your database with user2's information.
Simultaneously, user1 and user2 will subscribe to the chat room channel and monitor presence (rather than poll with hereNow) and receive the join events, as well. When either user leaves the chat room (unsubscribes from the channel) PubNub will send a leave event via web hook to your server and directly to the user that is still subscribed.
Once the last user leaves the chat room, PubNub will send a channel inactive event to your server and your server can invoke its chat room closed process to update your database as required.
This is a fairly high level design and there are some other details to consider but the message here is do not poll PubNub for presence information. Only use hereNow to get the current state of presence of a channel and listen for further presence events from that point forward either via web hooks to your server or via presence/subscribe to your clients apps. In implementation, you actually subscribe with presence (listen for presence events) and then call hereNow.
For a more detailed discussion of your requirements as it pertains to PubNub, I would recommend contacting PubNub Support to put you in touch with a Customer Success Manager and Solution Architect.

Sending data through php page from android form

I am helping one of my friend in his android app assignment. Here is the problem statement given in his android assignment.
Statement:- There should be proper implementation of POST method with queue to accommodate the data request even when device is in offline mode and execute data sync request from queue as soon as device is online again.
How this task could be done. Correct me if I am wrong, I am thinking that this requires to be done using SyncAdapter. If I am correct and if it has any other way then suggest the same with a tutorial link for implementation.
Yes, indeed you need to use SyncAdapter. Read more here.
Logically, you need to have a message queue, where you push new messages based on user events and possibly other events as well and you need to pop the messages from there and pass to SyncAdapter.

delay websocket updates and show the message later on?

I have an app where basically players challenge each other. At some point their challenge completes and I need to provide them (both of them - there are two players) with an update message, like 'Hey, you won and got 100500 points'. And vice versa - "Hey You looose"
I use websockets and pusher api to tackle the live updates, this works perfectly when player is "online". But what if they are not? The way to go for me looks like I can still handle the event with pusher and instead just displaying the message, I can store it to db to table challenge_notifications with fields messages and seen = 0. it's ok, but what would be the best way then to show this to the player when he comes online next time? I don't want to have ajax request on every page load checking to see if there are any unseen notifications for the user.
I probably somehow need to fetch all pending notifications only once, when they get online?
I use Laravel 5 for my backend.
There was a recent post on the Pusher blog about how to detect if a user is online or not using the Pusher HTTP API: Enabling Smart Notifications with Pusher and SendGrid.
The example uses SendGrid, but you could instead store the update to a database, send them a Push Notification, an SMS etc.
what would be the best way then to show this to the player when he comes online next time?
I guess there are two forms of "coming online":
The user is no longer on the site and has to navigate to the site. In that case as the page loads you can query the DB and serve them up any missed notifications directly (this would seem the easiest solution). Or, if it fits your app architecture, make a single AJAX request when the page loads to get any missed notifications.
If the user has gone offline due to them being mobile or having a bad network connection. In that case you can bind to the connected event using pusher.connection.bind('connected', function() {}); and then make a query to see if they've missed any notifications.
In summary: it would seem that querying the DB for any missed notifications upon normal page render (on the server) would be the simplest solution and wouldn't required much resource usage. But there are alternative mechanisms of delivering a notifications (email, SMS) if they're not online.

NodeJS with PHP application and user session

I have a PHP application (with Symfony2) and I need to let my users talk, and other tings, in real time (with socket.IO).
Let's focus on the chat mechanism : A logged user can talk with an other logged user (logged with FOSUserBundle in Symfony). When the user sent a message, it must be saved in MySQL and sent in real time to the other user. So the message is linked to two users (a sender and a receiver) in my MySQL database.
So I have two possibilities :
I use PHP to store messages :
I have an event on the click "submit" and call a PHP url with AJAX
If my PHP return "OK" (so he correctly added the message in database), I emit a Socket.IO event -> let doctrine deal with datas and symfony with my user
On NodeJS side, I have a listener on this event and I sent the message through Socket.IO with an other event
I use only NodeJS :
How can I log my user on PHP and NodeJS side ?
I need to use a different database for datas in real time ?
How can I share my users between NodeJS and PHP ?
I don't know what is the cleanest solution, if someone can help me.
Thanks !
Can be done using a common data store like redis to store user sessions
Check this link for reference
http://simplapi.wordpress.com/2012/04/13/php-and-node-js-session-share-redi/
Also check
http://www.slideshare.net/leeboynton/integrating-nodejs-with-php-march-2013-1
Which gives you an overview of how sessions can be shared using a common in-memory data store and some sample memcached code.
WebSockets
If you are using nodejs just for the WebSockets part, I'd suggest not to because then you'd have to replicate your user authentication logic in nodejs as well
You can instead do WebSockets in PHP using some library like http://socketo.me/
As far as storing the messages in MySQL goes
Your first approach of making an AJAX call to store the message in database, waiting for the response and then emitting a SocketIO event would lead to a sluggish chat experience.
I'd suggest you store messages to MySQL through NodeJS (asynchronously)
On a side-note check https://github.com/kyle-dorman/ChitChat.js for adding real-time chat functionality to your website.

Categories