Jump to content
MakeWebGames

Instant Messaging - What Language?


Coly010

Recommended Posts

I would like to make an instant messaging script so to speak, where the users can talk "instantly" with their friends or so on.

I tried doing this through php and jquery already, however the result was anything but instant. even trying to open a chat would cause the site to slow to a halt.

I have no problems with the concept behind it all, what I am asking, though, is, what is the "fastest" language, or combination of languages i can use for this. I was looking around and saw stuff like ASP and VB, as well as the usual javascript, html, html5, jquery and such.

the three main functions would be:

start a chat

chat box updates "instantly" when new message has been sent/received

"automatic" notifications when someone adds the user/ starts a chat with the user/ sends the user a private message.

store messages in a database or similar for referencing, and displaying

Is it possible to do this without causing the site to slow to a halt, because it will be practically useless if there are 1000 people online and the language results in the site being unusable.

I know about node.js but i havent fully looked into it. It seems really confusing even just to set up, never mind delve into.

any help is much appreciated.

Link to comment
Share on other sites

Sending small messages wouldn't normaly be a large problem for a server.

That's where they for. Just write good code that does what it should do and no more than that.

On the client side you could create this with AJAX/Java direct in your browser.

node.js looks a bit overkill for a chat client. HTTP is already supported by the browser, if you created it with JavaScript.

Only if you want to have your own transmission protocol 'node.js' could be a choise.

 

Coly010: is this for your: Bitten Text Based RPG ?

Maybe i also want to have a chatservice in my game.

Than you can have mine, but i'm also still `designing` the game. I'm only testing/programming the stockmarket part of my game/platform.

Link to comment
Share on other sites

Like i said, I tried this before with php, mysql, and jquery. but the site is overly slow! It is quite literally unusable when tested online. when i test on my computer using xammp it works fine, but i think that might be down to the computer.

I was thinking maybe, would HTML 5's new features help me along in this, in any way?

Lucifer, no its for a separate project but I was thinking that if it was possible to get it working then it shouldn't be too hard to implement into any game.

Link to comment
Share on other sites

Like i said, I tried this before with php, mysql, and jquery. but the site is overly slow! It is quite literally unusable when tested online. when i test on my computer using xammp it works fine, but i think that might be down to the computer.

I was thinking maybe, would HTML 5's new features help me along in this, in any way?

Lucifer, no its for a separate project but I was thinking that if it was possible to get it working then it shouldn't be too hard to implement into any game.

Sounds to me your problem isn't in the code (unless it's HORRIBLY coded) but with the server. No amount of clean code can account for a garbage server.

Link to comment
Share on other sites

Coly010: if you want i can look at it why it's so slow.

I'm building my own chatclient right now.

But it's just for fun and i always can reuse the library functions.

 

Happy hacking: Roger.

 

Ps: I have a server running... http://rogerkeulen.nl or http://fmmorpg.nl

So it's not a problem for me to install it. But only if it doesn't have to go into the root directory. (Don't want to mess up my server with scripts/expriments that can't be moved)

Link to comment
Share on other sites

Well since you want instant, ajax in javascript is probably not what you want to use since you would have to poll the server for updates. You could use javascript websockets, but I don't believe they are supported enough by browsers to make a cross browser compatible js websocket. I think your best bet would be to create a socket connection between a shockwave flash object and your php webserver.

After that you can build your whole chat UI in flash, or you can set it up to communicate with javascript, so you can make your whole UI in html/hmtl5.

Edited by bluegman991
Link to comment
Share on other sites

Well since you want instant, javascript is probably not what you want to use since you would have to poll the server for updates. You could use javascript websockets, but I don't believe they are supported enough by browsers to make a cross browser compatible js websocket. I think your best bet would be to create a socket connection between a shockwave flash object and your php webserver.

After that you can build your whole chat UI in flash, or you can set it up to communicate with javascript, so you can make your whole UI in html/hmtl5.

Don't use javascript but use javascript websockets? So you do want to use Javascript?

People these days. NEVER use flash unless it's for something like Flash games. Use NodeJS/Socket.IO

http://arminboss.de/blog/2013/tutorial-how-to-create-a-basic-chat-with-node-js/

Just google NodeJS chat tutorial.

Link to comment
Share on other sites

Hmmm, Don't think this is going to work for me.

I don't own a machine, i own just a simple webserver with that crappy PHP.

If i could install something else i wouldn't use PHP, and than i also have no problem using sockets my self.

I can open en close a socket with php. But it's not a program, it's a script with a maximal run-time.

I just going to poll for new messages, no problem. I really don't see a problem with that.

 

Happy Hacking: Roger.

Link to comment
Share on other sites

Don't use javascript but use javascript websockets? So you do want to use Javascript?

People these days. NEVER use flash unless it's for something like Flash games. Use NodeJS/Socket.IO

http://arminboss.de/blog/2013/tutorial-how-to-create-a-basic-chat-with-node-js/

Just google NodeJS chat tutorial.

When I say don't use js, I meant don't use js alone, because it doesn't have the right tools to create an instant messaging app.

I didn't tell him to use websockets. I said that if he wanted to use js, then use js websockets. But to be cautioned because cross browser compatibility is not very well with js web sockets...

He can not use nodejs unless he has his own server we are looking for a client side + php solution. And flash is used for a lot more than web games. In fact most js chat providers use flash communicating with js... So they don't have to poll their server using up all of their resources. ;)

Besides there arent that many options for listening to websockets that require using something that is most likely already installed on the persons computer AS an add on to their browser.

So a few to choose from are Flash, Java, JS WebSocket, SilverLight(Windows Only, I think? I don't even believe that many windows computers have this installed.) Anyone care to add to the list of languages that don't require an additional installation for the client side user?

P.S. !Angel... Why would you say to never use flash then suggest Socket.IO when Socket.IO uses flash?

Edited by bluegman991
Link to comment
Share on other sites

Why not try C. I think wsirc that alain wrote is with some sort of C

Again we are trying to find a solution for the client side, not the server side. Browsers do not support C for client side programming.

WSIRC uses Socket IO, which uses a combination of flash and websockets. If flash is installed on the users computer. Socket IO will use flash, if flash is not available Socket IO will use websockets. But yes that would be a good solution.

Edited by bluegman991
Link to comment
Share on other sites

When I say don't use js, I meant don't use js alone, because it doesn't have the right tools to create an instant messaging app.

I didn't tell him to use websockets. I said that if he wanted to use js, then use js websockets. But to be cautioned because cross browser compatibility is not very well with js web sockets...

He can not use nodejs unless he has his own server we are looking for a client side + php solution. And flash is used for a lot more than web games. In fact most js chat providers use flash communicating with js... So they don't have to poll their server using up all of their resources. ;)

Besides there arent that many options for listening to websockets that require using something that is most likely already installed on the persons computer AS an add on to their browser.

So a few to choose from are Flash, Java, JS WebSocket, SilverLight(Windows Only, I think? I don't even believe that many windows computers have this installed.) Anyone care to add to the list of languages that don't require an additional installation for the client side user?

P.S. !Angel... Why would you say to never use flash then suggest Socket.IO when Socket.IO uses flash?

 

Because Flash in general has various downsides, for one, mobile compatibility. Flash is not essential to Socket.IO

Link to comment
Share on other sites

A little something to start of your search for php and websockets.

https://code.google.com/p/phpwebsocket/

Ok so we know that php is not the language of choice when creating socket connections. But obviously php is all he has and is set on using it. Are you saying that php simply is not compatible with websockets, or are you suggesting that there is another option that he can use that is already enabled on his hosting account?

Link to comment
Share on other sites

Well... this creates a special server, so you can't run that simply via Apache as you usually run your PHP files. That need to be run on the server side ( http://code.google.com/p/phpwebsocket/source/browse/trunk/%20phpwebsocket/server.php ) and started with the PHP Cli environement. Sure thing, then it may work, but usually a shared host will not allow you to run your own processes. You will have more luck searching for a node.js hosting or an ASP.NET hosting and then implement the web sockets there.

This was the reason why I said PHP was not the best choice, because basically as Apache module (or any other usual web hosting solution) PHP doesn't let you server web sockets.

Link to comment
Share on other sites

With my old code, everything seems to work perfectly, until i open the chat box, then it slows down the site like completely. So here's the code for the chatbox and everything that affects it:

part of the header file:

echo '<script src="js/chat.js">
	</script>

	<script src="js/chat_input.js">
	</script>

	<script src="js/load_messages.js">
	</script>';

echo "<div id='footer'>
		<div id='chat_box'>

		</div>
		<div id='active_chats'>";
		include "active_chats.php";
		echo "
		</div>
                       <div id='pm_box'>

                       </div>
	</div>"

 

chat.php

<?php

include "mini_globals.php";


$fr_id = "";
$c_id = "";

if(isset($_POST['friend_id']))
{ 
$fr_id = $_POST['friend_id']; 
}
else
{
echo "FR_ID NOT SET<br />";
}

if(isset($_POST['ch_id']))
{ 
$c_id = $_POST['ch_id']; 
}
else
{
echo "C_ID NOT SET<br />";
}

$fr_id = abs((int)$fr_id);
$c_id = abs((int)$c_id);

$check_sql = $db->query("SELECT * FROM chats WHERE (c_id = '$c_id') AND (((user1_id = '$userid') AND (user2_id = '$fr_id')) OR ((user2_id = '$userid') AND (user1_id = '$fr_id')))");
$ch_nr = mysqli_num_rows($check_sql);

if( $ch_nr != 1 )
{
echo "Invalid Chat!";
}
else
{
$fr_sql = $db->query("SELECT * FROM users WHERE userid = '$fr_id'");
$fr_ar = mysqli_fetch_array($fr_sql);

$fdname = $fr_ar['display_name'];
$fdpic = $fr_ar['display_pic'];
echo "<div class='chat'>

<table width='100%'><tr>
<td width='90%' class='td_name'><center><b>" . $fdname . "</b></center></td><td width='10%'><img src='img/ex.png' class='hide_div' onClick='hideChat()' onMouseOver='HoverExOver()' onMouseOut='HoverExOut()' /></td>
</tr></table>
<div id='chat_messages' style='overflow-y:scroll;'>

</div>
       <div id='is_writing'>

       </div>
<div id='chat_input'>
	<input type='hidden' id='fr_id' value='" . $fr_id . "' />
	<input type='hidden' id='chat_id' value='" . $c_id . "' />
	<table width='100%'>
	<tr>
	<td width='100%'>
		<textarea id='imessage' cols='45'></textarea>
	</td>
	</tr>
	</table>
</div>

</div>";
}

?>

 

load_messages.php

<?php

include "mini_globals.php";

$fr_id = $_GET['friend'];
$c_id = $_GET['ch_id'];

$fr_id = $db->real_escape_string($fr_id);
$c_id = $db->real_escape_string($c_id);

$limit = 20;

$select_msgs = $db->query("SELECT * FROM i_messages WHERE c_id = '$c_id' ORDER BY im_id DESC LIMIT 20");
$imessage_array = array();

while( $imessages = mysqli_fetch_assoc($select_msgs) )
{
$imessage_array[] = $imessages; 
}
$imessages = array_reverse($imessage_array);

echo "<table width='100%' class='msg_table'>";

foreach($imessages as $im_ar)
{

if( $im_ar['s_id'] == $userid )
{
	echo "<tr class='user_sent_msg'> <td>";
	if( $im_ar['read_by_user'] == 1 )
	{
			echo "<img src='img/read_icon.png' />";
	}
	else if( $im_ar['read_by_user'] == 0 )
	{
                   if( $im_ar['received'] == 1)
                   {
			echo "<img src='img/received_icon.png' />";
                   }
                   else
                   {
		if( $im_ar['sent'] == 1 )
		{
			echo "<img src='img/sent_icon.png' />";
		}
                   }
               }

	echo "</td>";
	$time = strtotime($im_ar['time']);
	echo "<td width='85%'>" . $im_ar['message'] . "</td><td class='time'>" . humanTiming($time) . " ago</td>";
}
else if( $im_ar['s_id'] == $fr_id )
{
	$time = strtotime($im_ar['time']);
	echo "<tr class='friend_sent_msg'><td class='time'>" . humanTiming($time) . " ago</td><td width='85%' class='friend_sent_msg_msg'>" . $im_ar['message'] . "</td><td></td>";
	if($im_ar['received'] == 0)
               {
                   $im_id = $im_ar['im_id'];
                   $db->query("UPDATE i_messages SET received = 1 WHERE im_id = '$im_id'");
              }
}

echo "</tr>";	
}

echo "</table><span id='bottom_of_div'></span>";

?>

 

send_imessage.php

<?php

include "mini_globals.php";

$imessage = $_POST['i_message'];
$fr_id = $_POST['friend_id'];
$c_id = $_POST['ch_id'];

$imessage = $db->real_escape_string($imessage);
$fr_id = $db->real_escape_string($fr_id);
$c_id = $db->real_escape_string($c_id);

$iw_sql = $db->query("SELECT * FROM chats WHERE c_id = '$c_id'");
$iw_ar = $iw_sql->fetch_array(MYSQLI_BOTH);

$user1 = $iw_ar['user1_id'];
$user2 = $iw_ar['user2_id'];

if($userid == $user1)
{
   $db->query("UPDATE chat SET u1_writing = 0 WHERE c_id = '$c_id'");
}
else if($userid == $user2)
{
   $db->query("UPDATE chat SET u2_writing = 0 WHERE c_id = '$c_id'");
}

   if( $imessage == " ")
   {

   }
   else
   {
       if ( $imessage == "")
       {

       }
       else
       {
           $db->query("INSERT INTO i_messages(c_id, s_id, r_id, message, time, sent) VALUES('$c_id', '$userid', '$fr_id', '$imessage', NOW(), '1')");
       }
   }

   $limit = 20;

$select_msgs = $db->query("SELECT * FROM i_messages WHERE c_id = '$c_id' ORDER BY im_id DESC LIMIT 20");
$imessage_array = array();

while( $imessages = mysqli_fetch_assoc($select_msgs) )
{
$imessage_array[] = $imessages; 
}
$imessages = array_reverse($imessage_array);

echo "<table width='100%' class='msg_table'>";

foreach($imessages as $im_ar)
{
   $im_id = $im_ar['im_id'];

if( $im_ar['s_id'] == $userid )
{
	echo "<tr class='user_sent_msg'> <td>";
	if( $im_ar['read_by_user'] == 1 )
	{
			echo "<img src='img/read_icon.png' />";
	}
	else if( $im_ar['read_by_user'] == 0 )
	{
                   if( $im_ar['received'] == 1)
                   {
			echo "<img src='img/received_icon.png' />";
                   }
                   else
                   {
		if( $im_ar['sent'] == 1 )
		{
			echo "<img src='img/sent_icon.png' />";
		}
                   }
               }

	echo "</td>";
	$time = strtotime($im_ar['time']);
	echo "<td width='85%'>" . $im_ar['message'] . "</td><td class='time'>" . humanTiming($time) . " ago</td>";
}
else if( $im_ar['s_id'] == $fr_id )
{
               $db->query("UPDATE i_messages SET read_by_user=1 WHERE im_id = '$im_id'");
	$time = strtotime($im_ar['time']);
	echo "<tr class='friend_sent_msg'><td class='time'>" . humanTiming($time) . " ago</td><td width='85%' class='friend_sent_msg_msg'>" . $im_ar['message'] . "</td><td></td>";
}

echo "</tr>";	
}

echo "</table><span id='bottom_of_div'></span>";


?>

 

mini_globals.php

<?php

session_start();

if( $_SESSION['online'] != 1 )
{
header("location:login.php");
exit;
}

$userid = $_SESSION['userid'];

include "mysqli.php";
global $db;

echo "<script src='js/chat_input.js'>

</script>
<script src='js/load_messages.js'>

</script>

<script src='js/friends.js'>

</script>

<script src='images.js'>

</script>";

date_default_timezone_set('Europe/London');

$is = $db->query("SELECT * FROM users WHERE userid = '$userid'");
$ir = mysqli_fetch_array($is);

include "global_func.php";

?>

 

chat.js

function showChat(friendid, chat_id)
{
$('#chat_box').css({top:'50%',left:'50%',margin:'-'+($('#chat_box').height() / 2)+'px 0 0 -'+($('#chat_box').width() / 2)+'px'});

var fr_id = friendid;
       var c_id = chat_id;

   $.post("chat.php",
   {
       friend_id: fr_id,
       ch_id: chat_id
   }, function(data, status)
   {
       $("#chat_box").html(data);
       load_messages(fr_id, c_id);
       setInterval(function() {
           $("#is_writing").load("is_writing_div.php?ch_id=" + c_id + "&rnd=" + Math.random())
       }, 3000);
       $("#chat_box").slideDown();
   });

}

function newChat(friendid)
{
$('#chat_box').css({top:'50%',left:'50%',margin:'-'+($('#chat_box').height() / 2)+'px 0 0 -'+($('#chat_box').width() / 2)+'px'});

var fr_id = friendid;

$.post("newchat.php",
{
	friend_id: fr_id
}, function(data, status)
{
	var chat_id = data;
	$.post("chat.php",
	{
		friend_id: fr_id,
		ch_id: chat_id
	}, function(data, status)
	{
		$("#chat_box").html(data);
		$("#chat_box").slideDown();
	});
});
}

function hideChat()
{

$("#chat_box").slideUp();

}

function HoverExOver()
{
$(".hide_div").attr('src', 'img/ex_over.png');
}

function HoverExOut()
{
$(".hide_div").attr('src', 'img/ex.png');
}

 

load_messages.js

function load_messages(friend, chat)
{
           var refreshId = setInterval(function() {
            $("#chat_messages").load('load_messages.php?friend='+ friend +'&ch_id='+ chat +'&randval='+ Math.random());

           }, 3000);
            $.ajaxSetup({ cache: false });
}

function load_pmessages(friend, chat)
{
            $("#private_messages").load('load_pmessages.php?friend='+ friend +'&ch_id='+ chat +'&randval='+ Math.random());

}

 

chat_input.js

$(document).ready(function()
{
$('#imessage').autogrow();

$("#imessage").keypress(function(event) 
       {
           var friendid = $("#fr_id").val();
           var chatid = $("#chat_id").val();
           var holder = $("#imessage");

               if (event.keyCode == 13) 
	{
                       event.preventDefault();
		var msg = $(this).val();
                       post(msg, $(this), friendid, chatid);
	}


   });

   $('#pmessage').autogrow();

$("#pmessage").keypress(function(event) 
       {
           var friendid = $("#fr_id").val();
           var chatid = $("#pmc_id").val();
           var holder = $("#pmessage");
               if (event.keyCode == 13) 
	{
                       event.preventDefault();
		var msg = $(this).val();
                       postpm(msg, $(this), friendid, chatid);
	}
   });
});

function post(msg, selector, friendid, chatid)
{
       var c_id = chatid;
selector.val("");
$.post("send_imessage.php",
{
	i_message: msg,
	friend_id: friendid,
	ch_id: chatid
}, function(data, status)
{
           $("#chat_messages").html(data);
           $("#chat_messages").animate({scrollTop: $("#bottom_of_div").offset().top}, 2000);
           var refreshId = setInterval(function() {
           $("#chat_messages").load('load_messages.php?friend='+ friendid +'&ch_id='+ chatid +'&randval='+ Math.random());

           }, 3000);
           $.ajaxSetup({ cache: false })
});

}

 

Right so thats a hell of a lot of stuff, please dont comment on security of what's going on. i just want to know if you can see anything that could be causing it to slow down when the chat opens.

thanks :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...