Best way to make a chat

10 replies
Hi all,

I have recently developed a website where i created a custom chat, where there is a main chat room and then the ability to click on an user to chat privately.

I did it in the most simple way, with short polling, making ajax requests every 1-2 seconds to update the last activity date for keeping track of online users and to get the latest users in the chat room and the latest messages. It is done in PHP+MySQL.

This is definitely not the best way to do it and i know that, it was just my first chat app and i wanted to get it done asap with my current knowledge.

Now lets say the site will be having a few hundreds or even thousands of users at the same time, that would have a big impact to the MySQL, right?

Would there be any server that is not too expensive and that could support such amount of users?

Would you suggest of doing long polling? Or would that not be enough either?

Or would i need to use some other technology much better for this kind of apps, like NodeJS + MongoDB?

Looking forward to your suggestions.
#chat #make
  • Profile picture of the author emptee
    Hey mate,

    My advice would be to not reinvent the wheel if possible.. IRC has been around for decades and still works great!

    There are a few java clients, a few flash clients, and a few websocket clients around, which you could use straight away - otherwise if these don't suite you, you could write one up in JS easier than making a new system from scratch, in my opinion anyway...

    As an added bonus, your users would then also be able to connect using third party clients, which some of them may prefer.

    Otherwise, if you insist on doing it all from scratch.. and you don't want to use websockets.. long polling would be the way to go.

    Again, I would strongly urge you to not go down the mysql/mongodb route if at all possible - scaling could become an issue pretty quickly.

    Cheers,
    Michael
    {{ DiscussionBoard.errors[9959013].message }}
  • Profile picture of the author emptee
    Oh, I forgot to mention.. The HTTP/PHP stack probably wouldn't be much of an issue, even a smallish VPS should be able to put up with the request abuse.. it's the queries that will kill you, eg.

    Assuming you have your clients polling every 2 seconds, either from a long PHP session or from individual AJAX requsts..

    100 users @ 1 request per 2 seconds = 50 UPDATES + 50 SELECTS = 100 queries/second

    Scale that up a bit and you can see the problem! And we're not even talking about checking authentication, updating statistics... or even actually chatting yet!
    {{ DiscussionBoard.errors[9959038].message }}
    • Profile picture of the author mikea12
      Best way is socket.io hands down. I am available for hire, and can show you some realtime chat applications I have built. Working on one for android right now.

      http://postimg.org/image/ln7f79xct
      {{ DiscussionBoard.errors[9959213].message }}
  • Profile picture of the author dasoftcrew
    Thanks for your suggestions guys. I have one question: I heard a lot of people saying that websockets are not working in all browsers yet or something like that. Is it true? Or is there a workaround to make it work in all browsers?
    {{ DiscussionBoard.errors[9959835].message }}
  • Profile picture of the author sethczerepak
    Originally Posted by dasoftcrew View Post

    Hi all,

    I have recently developed a website where i created a custom chat, where there is a main chat room and then the ability to click on an user to chat privately.

    I did it in the most simple way, with short polling, making ajax requests every 1-2 seconds to update the last activity date for keeping track of online users and to get the latest users in the chat room and the latest messages. It is done in PHP+MySQL.

    This is definitely not the best way to do it and i know that, it was just my first chat app and i wanted to get it done asap with my current knowledge.

    Now lets say the site will be having a few hundreds or even thousands of users at the same time, that would have a big impact to the MySQL, right?

    Would there be any server that is not too expensive and that could support such amount of users?

    Would you suggest of doing long polling? Or would that not be enough either?

    Or would i need to use some other technology much better for this kind of apps, like NodeJS + MongoDB?

    Looking forward to your suggestions.
    You should look into this plugin...

    Facebook style jQuery plugin for chatting

    I've used it to build chat features in just a few hours, on multiple sites. Most of them on shared servers and it works just fine.
    {{ DiscussionBoard.errors[9974622].message }}
  • Profile picture of the author rosario1990
    I've been using chatwing on my site and it works very well, it has a ton of customization options and the really good part that i have not seen on any other software is it has mobile app so i have chatwing android app on my phone and i can get alerts when people send me private message or are chatting in my rooms. very cool.
    {{ DiscussionBoard.errors[9976224].message }}
  • Profile picture of the author Curtnielson
    Websockets are more "efficient" than long-polling in the sense that polling is still dependent on HTTP. This means all the overhead of HTTP, including headers, cookies, etc. is transmitted, whereas websockets won't send that extra information.
    {{ DiscussionBoard.errors[9983149].message }}
    • Profile picture of the author rosario1990
      Originally Posted by Curtnielson View Post

      Websockets are more "efficient" than long-polling in the sense that polling is still dependent on HTTP. This means all the overhead of HTTP, including headers, cookies, etc. is transmitted, whereas websockets won't send that extra information.
      May be right you're in some cases. I tried many of it. But can't find any thing like chatwing. It's superb! Chatwing has many more features for this it stunning from another one.
      {{ DiscussionBoard.errors[9993668].message }}

Trending Topics