2013-09-05

A 150 lines comet server written in C

Views: 8037 | 8 Comments

Comet is a web application model that used to “push” data to web page(browser), which can be used in applications like Web Chat, News Feed, Stock Market, even Web Game.

There are many opensourced comet servers, but most big companies develope their own comet server. As we know, Facebook’s comet server is built on Mochiweb, an opensourced Erlang Web server.

Nginx’s module nginx-push-stream is another solution, but it is suitable for small system, it can not handle 100K+ connections, according to our experiences. A comet should be able to handle 1 million connections.

Many companies develope their own comet server, for performance, and for integration with existing systems.

Writing a comet server is very easy with libevent, the famous network library. The simple C comet server is only 150 lines of code, can be down load from here: https://github.com/ideawu/icomet

Posted by ideawu at 2013-09-05 21:56:21

8 Responses to "A 150 lines comet server written in C"

  • Hello!
    When i use this app on login,i want to limit send 10 message to the client,what should i do? Reply
  • Hello,

    1. Unfortunately, but when I try to send URL encoded content get the error in javascript, because answer comes as decoded string with line breaks.

    2. No, I mean use CORS (XMLHTTPRequest) instead JSONP. I this case icomet should have OPTION /sub method with header Access-Control-Allow-Origin: * and ofcourse same GET method. Reply
    @Cackle: well, it is a bit complicated. the content must be json encoded string without leading and trailing quote marks.

    I made a change to the js api, now iComet supports pub() method, you can pub any string, but remember to write your own sub callback. Reply
    @Cackle: 1. Can you post your js code? You may look into the chat demo, it sends "\n", works right.
    2. not in schedule. Reply
  • Hello!
    I have more questions:
    1. How can I send json with \r\n in /pub controller? Can I call it like POST method?
    2. Can I use cors (Cross-origin resource sharing, OPTION method and GET with Access-Control-Allow-Origin header with *)?
    3. In case when we have more then 65K connections how icomet handle it? I mean we can’t support more then 65K ports per one IP.

    Thanks! Reply
    @Cackle: Hi,

    1. Say, you are using PHP to send “\r\n”, just urlencode() that string. HTTP POST method is not supported at this time, but will be supported at later version.
    2. You can send any addition header, icomet will just ignore them.
    3. It is a common false thought on "65536 sockets", TCP socket server accept all client sockets on the SAME port. The kernel uses a {local_ip, local_port, remote_ip, remote_port} tuple to determine a connected socket. So with a single IP:port, a lot more connections is supported, up to 4G x 65K connections! Reply
  • Hello!
    Thank you for great apps!
    Can I increase max channels and subscribers per channel to 100K in https://github.com/ideawu/icomet/blob/master/comet.conf ? Reply
    @Cackle: Yes! Of course you can. Reply

Leave a Comment