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/.
Related
I'm trying to push my skills with PHP and SQL that I've recently learnt at college; I'm making a website that I can chat with some friends on. I know how i'll do everything ( log on, save messages & display messages ) but I'm unsure of how to make the messages constantly load instead of having to reload the page like on Facebook.
If I was to use this: while($sqlRow = mysqli_fetch_array($sqlResult)){
Would that keep loading messages as they're added to a table for holding messages?
I think the easiest way to implement this is to use web sockets: think of a web socket as a two-way communication channel that is always open between your client and your server. Even though your client is done loading the page, your server can still send messages to it through this open channel, at any time.
I have never used web sockets with PHP (only with javascript and node.js), so I can't really help you with how to actually implement this system. However I found this article that may help you: http://www.phpbuilder.com/articles/application-architecture/optimization/creating-real-time-applications-with-php-and-websockets.html
Let me know if that helps.
I'm trying to understand how I can send data from a web server to my android app. This isn't a request/receive type question, what I am looking to do is basically have my web server (say for example www.example.com/index.php) send a data (like a string for example) to my android app. Now, this data can be sent anytime, it isn't something where a you click something on the app to receive the data, but rather, when the server is wanting to send data, it sends the data, and my app picks it up. Now I'm not looking for my app to be listening for this data every second, an interval of 5 minutes or so is fine.
The question being, how would I achieve something like this? How can I send a string containing "Hello World" to my app? I know PHP, and I know this has to be done using POST/GET, and of course I know there will probably be some type of listener, service, or something of sort on the android side listening for this data, but I just want to know how I can approach this. What should my setup be?
If someone can provide some link or code to send a basic String message, then that would be more than enough for me to learn from.
There is two best approach you can follow.
from server to app: in this case make corn job(timer service) which send push notification to android app. when app get push notification create one background service which download any data from server.
Note: if you have small text with limit of 100 char then no need to create service just send using push notification
fetch data from server: in this case make one background service(auto start service) in android app. which run within some interval period like 1 hour or more(whatever your requirement). that service fetch data from server.(in this case android app eat battery of user device because of background service)
i will suggest you to use 1st approach because its easy and best way(in case of performance) your server do everything & android app have to just receive data.
how to implement push notification best tutorial using PHP & android(its old but just read official docs)
For this you need implement android push notification.When you send the data from php server then the application get this data automatically.
yes if you want to send data into your app from server side to app without calling web service on click of button you will use cronjob which run on defined time from server side(As i.e : if you want to run it every five seconds or minutes it will possible you don't need to call it from app side).
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.
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.
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.