I have a website running on shared server with apache/mysql/php. Each of the pages of the site belongs to one registered user and this user,once logged in can edit the page. Other users that are not owners of the page can only look at these pages but cannot do anything else.
Now I'm planning to add chat functionality to my application. The basic idea is that if owner of the page opens it in a browser, and is logged in, he will be shown for other users (that will be anonymous) as "available for chat". Other users visiting the page will be able to send him messages and vice versa. Anonymous users do not need to communicate with each other and they can communicate only with registered user (owner of the page). So basic structure would be like that:
anonymous user(s) visits the page. Registered owner of the page is not looking at the same page and chat is not available.
Registered owner logs in and opens his page in a browser. All registered users in real time are informed owner is available for chat now.
Anonymous users can send him messages.
Registered user receives the messages and can respond back to each user
Other users can join and chat with registered user any time. It all happens in real time and registered user can see who comes in to visit his page and goes away.
Now, in step 3 and 4 I need to know if the registered user is still logged in. If so then the messages can be passed further to intended user. If not then instead I need to send a message that the owner (registered user) is no longer available for chat.
I'm looking for advice on how to best implement it:
using old school php and ajax calls. So every user would send ajax request every second or so to server and server would keep track of each conversation somehow. Relatively easy to implement I think. I'm not expecting large number of users but I can imagine this would be heavy on the server.
using node.js.
Now my questions:
What could be possible problem with solution 1 above. Would that be too heavy on a server constantly throwing ajax requests at it? would would be reasonable number of users I could accept?
Using node js on my shared hosting.. assuming its possible to install it and run it on separate port, how would I best go about checking if registered user is still logged in or not? Any ideas would be much appreciated as am out of ideas here.
You're right that the PHP/Ajax calls can cause quite a bit of server load, especially if your Apache/PHP stack needs a lot of memory to bootstrap. Many chat modules in PHP systems, e.g. Drupal, actually offload this responsibility onto a specialized node.js server (the second approach you mentioned) to facilitate scaling.
Another approach you may consider is to use a real-time network such as PubNub to facilitate this user-to-user data transfer. PubNub has a toolkit called Presence which can help with telling who is subscribed or unsubscribed to each channel.
To fit this to your requirements, I imagine that each user will register with the page they are viewing upon landing on it, by issuing this call in your JavaScript:
<script src="https://cdn.pubnub.com/pubnub.min.js"></script>
<script>
var pubnub = PUBNUB({
uuid : '12345-page35' //You can define this for each user
})
pubnub.subscribe({
channel : 'site-wide-chat,page35', //Subscribe to two channels!
message : receive_chat, //Callback function
presence : user_joined //Callback function
})
</script>
When the "owner" logs in the other users viewing the page are notified. You can accomplish that like this:
function user_joined(event) {
if (event.uuid.match(/page35/)) { //You can set your own test here
// .... admin available for chat
}
}
Presence also has a bunch of nifty features such as the ability to get all users subscribed to the current channel:
pubnub.here_now({
channel : 'page35',
callback : function(m){console.log(m)}
});
I hope this helps you build your minimum viable product. Since everything is implemented at the programming language level, you should have a lot of flexibility crafting a customizable chat solution without adding additional complexity or overhead on your server.
Related
I Have a Laravel based application, I want to add a notification system like Instagram and other social networks. how I should implement the Database and PHP to achieve this goal?! (I just want the base knowledge about implementing this functionality, not the code)
for example: when someone does some action about your profile: a new notification send to you and you noticed that for example "John created a new Deal with you".
there are dozen of push notification libraries and also server side programs you can use. they support web notifications also.
and if you want to create your own notification system (for any reason) the story differs. but recommended way is to use those libraries and not to bother yourself!
i think there should be two notification system type: static and dynamic.
in static system you save each message for user and whenever he logged in you show him all messages. you can utilize session for saving and manipulating messages.
in dynamic way, like a stream based way, you just save the time user saw the messages last time and when user logged in you will make the messages dynamically and show him those.
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.
I'm currently creating a web app. The app goes like this, the client creates something like reports in the web app, and we are using html5 and php and mariadb w/ phpmyadmin for our front end and database. And then, the report will send to the server. And the admin of the server will give response to the report. Now I am wondering on how the admin will response. Should I create a new webpage for the admin so he can receive the report? Should I use websocket? or is there something else? Any idea? Thank you very much in advance.
Since a web page report submission is inherently asynchronous (ie you don't know when it will occur), it follows that the admin response to submitted reports should also be asynchronous (since admins have to go to the toilet every now and then, much to the chagrin of management).
Your report system should post the report to a queue of 'to-be-processed' inbound reports. The admin could have either a web page portal (or a custom native applicatoin) for viewing pending reports. From there the admin can select one, view it, send client an update, close it. in other words, whatever host of actions the admin is expected to perform.
In this manner you can have a super admin interface that monitors one or more admins responding to reports and offers a higher level view of all the pending reports (time on queue) and the number of closed reports per admin per hour/day .. you could even make a graph..everyone likes graphs.
hope that helps.
I'm trying to learn the development of Web Applications in JavaScript, and for this I am developing a simple Time Tracking application. I am developing this with ExtJS for dynamically creating the UI.
This would allow Employees to submit the time they have spent working on different projects, and allow the Managers, to add Projects to Users and so on.
Once the user signs in, I determine their role, and provide the appropriate UI (through JavaScript).
I was wondering what is the best and most secure way of doing this? (I am of course checking the authorization on the server side, so that no one can make changes by just calling my PHP Services via http get/post)
I asking from a perspective of disallowing a non authorized person, to even see the non-authorized UI, by fiddling with the JavaScript (from the FireBug console for example).
I was thinking of creating a Service which returns the appropriate script for creating the appropriate UI, through JSONP. It feels quite WTF-ey to me, so I was wondering if there was better way.
To build on naugtur's comment. You must always assume that there will be a very smart person that can build the UI for himself/herself, or fake any of the calls to the server that any part of the UI could do.
Based on that, the premise of the way you suggested towards the end of your question seems debunk to begin with. The easiest route once we settle that (I'm assuming your making a single-page application) would be to always dump all the javascript code right up front at the login screen and only allow the user to see what they need to based on their role (i.e. a card panel that has different cards for each role would be an example).
I would also like to add that on the backend of the application, you should always have security checks in place to make sure the user has the correct role for whatever action he is doing. If your application is internet facing, this is a necessity, there will always be that user who will look through your code and see what he can do maliciously or just for fun... or that guy on who wants to see what happens when he uses the debugger to create his own fake ajax calls with varying parameters.
I developed a simple application on Facebook using PHP and I want to know how the people can add this application, and after adding the application a update is generated (e.g vipin join this application).
Thanks
Users don't add applications anymore. The closest they can get is bookmarking, which your application receives no notice of. The closest thing you can get to checking as a developer is checking is Users.isAppUser.
You also don't receive notice when a user grants/rejects permissions (which is generally a prerequisite to viewing your application).
Basically, you have to post notices and create feed entires in direct response to user action (unless they've granted you a perpetual session).
The old model of actually adding applications was all kinds of broken from a user experience point-of-view, so Facebook basically pulled the plug on it.
Yup, no more "Dude just added this app" messages in the newsfeed. However, Facebook will occasionally show "Dude uses the 'Thing' application" messages under the "highlights" section of a user's homepage. There's nothing you can do to encourage or discourage it -- it just happens.