Ajax response with two open session - php

I'm trying to build a chat application using short polling with ajax/php.
the problem that I have is when a user is connected in two devices (or two open session) he doesent receive a notification that he received a message.
is that problem related to the Ajax?

Related

browser stuck while sending many sms in background at a time in php for particular website

when i send many msgs at at time in background my websites is stuck until my request is complete . I am sneding msgs using php in background. How to get rid of this if request is in process mode we can easily access the websites ..??

How can I use websocket with the help of pusher to get data from database?

I'm using websockets with pusher , PHP and some Javascript. I want display some information from my database to my client. My goal is to refresh the client when an information has insert into my database.
but am facing some problems ..
how can combine between pusher and websocket to get those data ?
how can I get data from database using websockets and a pusher for doing a real time web application that whenever i added data to the database by default they will be shown in my client page ?
In your code where you are inserting information into the database, you can also trigger an event containing that data to Pusher.
It's up to you which channel to send the event on. You could have a channel per table, or a channel per row.
Then in your browser-side JavaScript you can subscribe to any channels you wish to receive updates on. See here for documentation on how to do this.

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.

Open a PHP page/initialize Session Variables Simultaneously?

I'm pretty new to PHP and JSON/XML, but we're forging ahead.
Our project entails logging in to a webservice (which we also built) from a handheld device and then accessing our website from that device. The point is to only allow users who have logged in on the handheld device to access the webpage.
Now, I guess this doesn't only apply to handhelds, but here's the question:
We can send an HTTPRequest from the handheld device and use a PHP script to validate login info from a database, and then return either a 'success' or 'failure' JSON object. If the user succeeds in logging in, our webservice will be opened in a browser window.
The online application relies on Session Variables for authentication every step of the way. Is there some way we can send the HTTPRequest, receive authentication, and then somehow feed the very same page we are opening some data which it will parse into session variables?
It depends on the device. Some mobile phones (like flip phones for example) will most likely not have support for AJAX. Smartphones, on the other hand, will most likely have support for AJAX.
To make it compatible with all devices, your best bet is sticking with a standard login system that posts the variables to the same page or another page for validation. Otherwise, you will be making several different websites to work on various phones.

Categories