delay websocket updates and show the message later on? - php

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.

Related

Quickbooks PHP API Generating Invoices On Order Submission

I have a client that wants to take orders via an online form, with the idea being that an order can be submitted and stored in a database via my application while simultaneously generating an invoice on submission in QuickBooks.
How do I do this in PHP when the person entering in the order is not the client but a client of the client? It seems like Quickbooks uses Oauth tokens and a javascript library to generate them to connect a company to an app, but I'm simply writing a backend for one company and want that backend to create invoices when saving an order. How do I think about this?
I'm not interested in anyone having to hit a button that says "connect to quickbooks" especially not the person filling the order because again, that person is a customer and doesn't need to know about the internals of the customer's invoicing system.
I just really want to use the Accounting API to generate invoices. Is there no way to simply link my backend to my one company directly in the Quickbooks SDK configuration and achieve this, or do they need to use a javascript library to get tokens. I'm unclear about what direction I should be going in and don't want to waste time with a client-side library if I don't need it to do backend logic.
Here's some example code that does exactly what you're looking for:
https://github.com/consolibyte/quickbooks-php
Along with a quick-start guide:
http://www.consolibyte.com/docs/index.php/PHP_DevKit_for_QuickBooks_-_Intuit_Partner_Platform_Quick-Start
Also see notes about your comments below -- you're on the right track, you're just misunderstanding how OAuth works:
It seems like Quickbooks uses Oauth tokens and a javascript library to generate them to connect a company to an app, but I'm simply writing a backend for one company and want that backend to create invoices when saving an order.
Correct, Intuit uses OAuth, and a little Javascript thing to kick off the OAuth process.
I'm not interested in anyone having to hit a button that says "connect to quickbooks"
Someone needs to hit this button... BUT only ONE PERSON needs to hit the button ONCE, EVER, and then NEVER again.
The owner of the company (e.g. your boss) needs to click the button ONCE, which gives the OAuth creds (and the realm ID) to you. Once your boss has done this ONCE, then you have the creds to use forever, for all of the actual customers.
Your customers (e.g. the people actually checking out/placing orders) DO NOT click any buttons, nor do they see or have any idea at all that you're even using QuickBooks.
just really want to use the Accounting API to generate invoices.
Cool, you can totally do that!
Is there no way to simply link my backend to my one company directly in > the Quickbooks SDK configuration and achieve this, or do they need to use a javascript library to get tokens.
Follow the quick-start above. It should take you about 15 minutes to get a working OAuth connection, and then you never need to use the client-side stuff ever again.
You only need to authenticate every 180 days btw.
If you use the reconnect script, you only need to authenticate ONCE, and can automatically renew the tokens every 180 days, no user-interaction required.
https://github.com/consolibyte/quickbooks-php/blob/master/docs/partner_platform/example_app_ipp_v3/reconnect.php
Well with the realm_id for example, I don't understand how that relates to ouath.
The realm ID is just a unique identifier for the particular QuickBooks Online company you're trying to connect to. Yes, you need to store it. If you use our libs, this is done for you automatically.
I guess I don't understand if I'm developing for one client why can't I just get their realm_id from them and then keep using it rather than making them do some form of authentication?
Again, they only have to authenticate ONCE. That's Intuit's way of giving you the realm ID and credentials you need to connect. Once you've done it ONCE, you never need to do it again. It takes all of about 30 seconds.
If they were to just give you OAuth creds without any authentication, it would be a gigantic security hole. If you read the Wikipedia article on OAuth it talks in depth about this, and the goals of OAuth.
Okay I think I get it, so they have to authenticate once every 180 days?
Once every 180 days, UNLESS you use a reconnect script, in which case they just authenticate once and then never ever have to worry about it again.
So I can store the token and the realm_id in a database before it expires and just use that?
Yes.
In this way my client can authenticate and then my scripts can generate invoices for them when their customers visit our website?
Yes!

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.

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.

Chat App with php, Jquery

I want to make a chat application with php & jquery. But jquery script visible to client side and another problem is every time need to update chat display panel by calling interval methods. So my question is, is there any other way to develop a chat app like gmail chat app.
Becouze gmail chat is show presence. When user went to offline automatically shows offline status. and when user entered text into chat box, instantly shows in chat display. so i wanna make to like that application.
Please guide me....
Thanking you,
You can do this with WebSockets. There are some cool WebSockets tools out there like:
Ratchet - http://socketo.me/
Wrench - https://github.com/varspool/Wrench
phpwebsocket - https://code.google.com/p/phpwebsocket/
apache-websocket - https://github.com/disconnect/apache-websocket
Using WebSockets you can append received messages to the chat log instead of updating the whole thing like it seems you are doing.
In case you choose to (or have to) keep requesting new messages to the server, since not all hosting providers will allow WebSockets, here are some tips that you may find useful to improve your chat app:
Store the last received message id on client-side, so that when you request new messages to the server you can send it this id and it will only send you messages you didn't receive yet, thus avoiding unnecessary traffic.
On the server side, record the last time a client requested new messages, so that you can define a timeout in order to detect user disconnection.
To avoid overloading your server or client with more requests than it can handle, take into consideration the time took by the server to answer your last request when you define the interval for the next request, like this:
Client requests messages
Server replies in 100ms
Client waits 100ms before requesting again
Server replies in 200ms
Client waits 200ms before requesting again
...
In order to update the status and message real time without polling, you need to use websocket connection.
Here is a jsfiddle for building a chat using Applozic jquery chat plugin which uses websocket.
https://jsfiddle.net/devashishmamgain/L68teL67/
(function(d, m){var s, h;
s = document.createElement("script");
s.type = "text/javascript";
s.async=true;
s.src="https://apps.applozic.com/sidebox.app";
h=document.getElementsByTagName('head')[0];
h.appendChild(s);
window.applozic=m;
m.init=function(t){m._globals=t;}})(document, window.applozic || {});
window.applozic.init({userId: 'devashish', appId: 'f769902edce1e93b6d03a1d5f', desktopNotification: true, notificationIconLink: "https://www.applozic.com/resources/images/applozic_logo.gif"});
Look on Below questions..
Javascript based XMPP chatclient using strophe js - Examples and tutorials?
You can find your requirement related to chat using stropher js for XMPP protocol on this below working github code.
https://github.com/metajack/profxmpp
Look on chap :06 demo (GAB Tut).
It will satisfy all your requirement related to
one to one chat.
Roster list (Friend list),
Send friend request,
In coming request approval.
Start one to one chat etc...
and all important demo also included
Let me know you have any query in this demo. :) :)
Yes if you use PHP to fetch data from server side then you would need to poll and check for new message at a regular interval. This creates unnecessary load on the server side and proves difficult to scale because we keep polling even when there is no new message.
This problem may be solved by using a push technology like websocket instead of polling. Our product, ChatCamp uses push technology to deliver messages in real time and is highly efficient and scalable. You may use our ChatCamp JavaScript SDK to quickly create a jQuery chat application - https://chatcamp.io/blog/jquery-chat/.

how to implement facebook like notification?

I am trying to implement a Facebook-like live notifications system to notify users whenever someone adds them as friend, like their post or posts replies to their comments.
All the database and PHP part is done, but I can't figure out how to implement it like Facebook has.
Whenever someone likes/comments on your post in Facebook the light blue box appears at the bottom left corner of the screen. This happens as soon as someone hits like button or posts comment in Facebook. I would like to know what I should do to implement this.
Using YUI or any JavaScript framework I can query a database table after n seconds to check for notifications. This method is too heavy.
I was wondering if there is any server side mod or scripting can be done so that whenever there is new notification entry in my database table the server will tell that particular client. That way unnecessary request calls from client to server will be avoided completely and system can work efficiently for website with more than 50,000 users online at a time.
How can I achieve this?
You should look into COMET techniques, such as forever frame (tutorial) and long polling. That allows you to have a form of a server->client push communication.
I am really surprised nobody has mentioned PubNub and Pusher
These two (competitors) are building infrastructure which allows for realtime notifications, just like Facebook.
Facebook notification
You basically set a request up, like callng the service that asks your server/db for the notifications of that user. You may do a while loop that retries if theres no notification (maybe Thread.Sleep in between searches). Your js request will timeout, then you can call the function again in timeout. This means long polling afaik
The only way to do it is to have some sort of mechanism (e.g. Javascript) to repeatedly poll the server for updates. Doing server pushes to web browsers isn't possible.

Categories