2

Need some direction on this. I'm writing a chat room browser-application, however there is a subtle difference.

These are collaboration chats where one person types and the other person can see live ever keystroke entered by the other person as they type.

Also the the chat space is not a single line but a textarea space, like the one here (SO) to enter a question.

All keystrokes including tabs/spaces/enter should be visible live to the other person. And only one person can type at one time (I guess locking should be trivial)

I haven't written a multiple chatroom application. A simple client/server where both are communicatiing over a port is something I've written.

So here are the questions
1.) How is a multiple chatroom application written ? Is it also port based ?
2.) Showing the other persons every keystroke as they type is I guess possible through ajax. Is there any other mechanism available ?

Note : I'm going to use a python framework (web2py) but I don't think framework would matter here.

Any suggestions are welcome, thanks !

PlanetUnknown
  • 3,956
  • 4
  • 49
  • 67
  • By headers you mean the name of the person ? e.g. "Scott : Hello this is my base". Hence "Scott" is the header ? – PlanetUnknown Apr 15 '11 at 03:37
  • You're basically trying to create google wave by the sounds of this. Not sure if you've heard of google wave....but it was kind of a failure. Maybe just use some of the wave technology? I think google shared that stuff? – wilbbe01 Apr 15 '11 at 03:41
  • Sorry, I deleted that comment. Look at the answer below. –  Apr 15 '11 at 03:42
  • wibbe01 - yes its something like wave, but only for text for now. – PlanetUnknown Apr 16 '11 at 11:53

2 Answers2

1

You could try doing something like IRC, where the current "room" is sent from the client to the server "before" the text (/PRIVMSG #room-name Hello World), delimited by a space. For example, you could send ROOMNAME Sample text from the browser to the server.

Using AJAX would be the most reasonable option. I've never used web2py, but I'm guessing you could just use JSON to parse the data between the browser and the server, if you wanted to be fancy.

  • I see what you are saying, but if text in the middle of the a paragraph is changed it'll be difficult to track. Now that I think about it maybe I'll have to have a queue to show the edits as they come in from the client. – PlanetUnknown Apr 15 '11 at 03:51
  • You could just send the whole paragraph again. –  Apr 15 '11 at 03:52
  • Thanks, let me think over that. Any idea about the first part of the question ? Is port based the only option ? – PlanetUnknown Apr 15 '11 at 03:58
  • Not really sure what you mean. I wouldn't recommend having a port for every room, but instead just sending the room you want before the text is sent... –  Apr 15 '11 at 04:01
  • I think I understand what you are saying. You mean the server takes all responses and then pushes it to all other clients to simulate realtime. Wonder how this "push" from server would really work. I think the client will actually be polling for messages for that chatroom name. Does this make sense to anyone ? – PlanetUnknown Apr 15 '11 at 04:06
  • Yeah, I see what you mean... the more I think about it, the more it confuses me. If you had it polling, it would just cost so many requests to the server, but I can't think of any other way really. You might be able to use HTML5's sockets, but I honestly have never used those and I don't know how they work. –  Apr 15 '11 at 04:09
  • Since a significant portion of your development will be in Javascript for the front-end anyway, you might look into WebSockets. – chaos95 Apr 15 '11 at 07:45
1

The Wikipedia entry for Comet (programming) has a pretty good overview of different approaches you can take on the client (assuming that your client's a web browser), and those approaches suggest the proper design for the server (assuming that the server's a web server).

One thing that's not mentioned on that page, but that you're almost certainly going to want to think about, is buffering input on the client. I don't think it's premature optimization to consider that a multi-user application in which every user's keystroke hits the server is going to scale poorly. I'd consider having user keystrokes go into a client-side buffer, and only sending them to the server when the user hasn't typed anything for 500 milliseconds or so.

You absolutely don't want to use ports for this. That's putting application-layer information in the transport layer, and it pushes application-level concerns (the application's going to create a new chat room) into transport-level concerns (a new port needs to be opened on the firewall).

Besides, a port's just a 16-bit field in the packet header. You can do the same thing in the design of your application's messages: put a room ID and a user ID at the start of each message, and have the server sort it all out.

The thing that strikes me as a pain about this is figuring out, when a client requests an update, what should be sent. The naive solution is to retain a buffer for each user in a room, and maintain an index into each (other) user's buffer as part of the user state; that way, when user A requests an update, the server can send down everything that users B, C, and D have typed since A's last request. This raises all kind of issues about memory usage and persistence that don't have obvious simple solutions

The right answers to the problems I've discussed here are going to depend on your requirements. Make sure those requirements are defined in great detail. You don't want to find yourself asking questions like "should I batch together keystrokes?" while you're building this thing.

Robert Rossney
  • 94,622
  • 24
  • 146
  • 218
  • Thanks Robert that was exactly what I was looking for ! I'm going to google it, but do you know off hand for what duration a long-poll from a client to the server will stay alive ? – PlanetUnknown Apr 16 '11 at 13:26