Here is a simple way to leverage the power of rules, views bulk operations, private message, private message growl and shoutbox to create a simple chat solution for your site. Site runs on shared server with limited resources and php 5.2
Please note that after one month of using this solution I found envolve chat module which is what I am using on my site now as it reduces the burden on the system.
- One single public chat on the site which auto updates
- Notifications to active logged in users on the site only
- Per user ability to opt out from the notifications messages
Step 1: Module Requirements (D7 versions):
Install all these following instructions at the module pages. Enable the private message rules and shoutbox rules modules too. Also go to Configuration > User settings > Add fields and add a simple boolean field titled I don't want to receive shoutbox notifications or something like this. This will be useful to produce an opt out feature for selected users later on. I kept the On value as 1.
Step 2: Shoutbox configuration:
Make sure that the shoutbox page shows the appropriate number of posts and auto-refreshes at a given interval. In found 60 seconds to be an acceptable compromise.
Step 3: Creating View and adding View bulk operation field
Create a new view showing users. In the fields add a views bulk operation : user field. Add an additional filter Last access and configure it to show greater than or equal to -10 minutes. This filters the list to users who have browsed the site in last 10 minutes. Depending on your requirements you can filter this further.
Add two more filters filtering the custom boolean field you added to the user profile. The first one will filter if value is null (useful if a user has not checked or unchecked the field - that is value of the field is null) and second will filter if value is 0 (that is user has not opted out of shoutbox notifications in his profile). Click on and/or rearrange beside filter list - create a new or group and drag the last two filter in that new box. So the whole filter query should look like this:
Users who are active AND Users who have last access >= -10 min AND (User: I dont want to receive shoutbox notifications = null OR User: I dont want to receive shoutbox notifications != 1)
Check if the view is showing the correct user (check against the online now views box for example).
Step 4. Create the rule
Add a new rule and make the event as A shout is added - in the action section do the following:
Click on Add an action > Scroll down to views bulk operations > Load list of entity objects > Select the view you created in step 3 above (simply select the master). Give a descriptive variable name like logged in users.
Click on Add loop > parameter should be logged in user (or what ever variable name you choose to give earlier). Add a descriptive term in list item - I added logged in username
Click on Add action beside the loop > Send private message. This will ensure that private message gets sent to all users in the view only. The recepient should be logged-in-username (the name of the list item you added in the step 2 above). From field should be user (or the shoutbox message actor - in essence the person who is shouting in the shoutbox). Customize the subject and body using the tokens provided. Here is the configuration I have:
Subject : New Shout by [user:name] !! (#[shout-message:shoutbox-message-id])
To respond visit the <a href="/shoutbox"> <span> The Shoutbox Page. </span></a>
<em>If you dont want to receive such notifications click this link [logged-in-username:edit-url] to access your profile page and make sure that <b>"I dont want to receive shoutbox messages field"</b> is checked.</em>
Step 4: Test the rule you have created you should receive a private message if you have shouted and as also any other user who is logged in.
Step 5: The private message jgrowl module can then be used to simply send notification to all active logged in users using a nice ajax popup without them having to refresh the page. You can also use other modules that use private message to send this message to the user.
I use this module in a member only site with few members interested in chatting. The private message module essentially fulfills the role of private chats quite nicely. This is just a feature to ensure that all active users get notified if a user is posting in the shoutbox. Note that while shoutbox does provide a block that can be shown on all pages it consumes a lot of resources if this is shown on all pages as it refreshes in the same time as the main page.