PHP & open connections

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lenard Redwood

    PHP & open connections

    I'd like to create a simple chat application that will leave the
    connection open for a minute and then restart the connection to the
    server to push data. I plan to use temporary .txt files to store
    messages. My question is how can I do that in PHP 5, what techniques
    should I do server-side to keep this connection open and how would be
    the flow of the message? Do I need a different technology such as
    Python to handle messages?

    1. User A sends a message to the server
    2. ?
    3. User B receives the message instantly through an open connection

    Any tip/link/tutorial would be greatly appreciated.
    Thanks,

    Lenard.

  • Jerry Stuckle

    #2
    Re: PHP & open connections

    Lenard Redwood wrote:
    I'd like to create a simple chat application that will leave the
    connection open for a minute and then restart the connection to the
    server to push data. I plan to use temporary .txt files to store
    messages. My question is how can I do that in PHP 5, what techniques
    should I do server-side to keep this connection open and how would be
    the flow of the message? Do I need a different technology such as
    Python to handle messages?
    >
    1. User A sends a message to the server
    2. ?
    3. User B receives the message instantly through an open connection
    >
    Any tip/link/tutorial would be greatly appreciated.
    Thanks,
    >
    Lenard.
    >
    You can't. HTTP is a pull only protocol. You can use javascript on the
    client to pull the info on a regular basis (i.e. every 30 seconds). But
    you can't push the data out.

    The other alternative is something like a Java applet where you can
    maintain a connection.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Lenard Redwood

      #3
      Re: PHP & open connections

      Jerry Stuckle wrote:
      You can't. HTTP is a pull only protocol. You can use javascript on the
      client to pull the info on a regular basis (i.e. every 30 seconds). But
      you can't push the data out.
      Exactly, actually each time user A writes a message and hits "Enter" it
      is sent right away to the server. But how can this message be thrown
      into the open connection that user B is using?
      Thanks,

      Lenard

      Comment

      • Jerry Stuckle

        #4
        Re: PHP & open connections

        Lenard Redwood wrote:
        Jerry Stuckle wrote:
        >
        >>You can't. HTTP is a pull only protocol. You can use javascript on the
        >>client to pull the info on a regular basis (i.e. every 30 seconds). But
        >>you can't push the data out.
        >
        >
        Exactly, actually each time user A writes a message and hits "Enter" it
        is sent right away to the server. But how can this message be thrown
        into the open connection that user B is using?
        Thanks,
        >
        Lenard
        >
        That's what I'm saying. You can't with the http protocol. It's not
        designed for that.

        Use javascript to update regularly or another tool such as java which
        can define its own protocol (java on the server and an applet on the
        client).

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • Kimmo Laine

          #5
          Re: PHP & open connections

          "Lenard Redwood" <freakingcrazy@ gmail.comwrote in message
          news:1159809523 .767613.115970@ m73g2000cwd.goo glegroups.com.. .
          Jerry Stuckle wrote:
          >You can't. HTTP is a pull only protocol. You can use javascript on the
          >client to pull the info on a regular basis (i.e. every 30 seconds). But
          >you can't push the data out.
          >
          Exactly, actually each time user A writes a message and hits "Enter" it
          is sent right away to the server. But how can this message be thrown
          into the open connection that user B is using?
          Thanks,

          Shared Memory Functions


          Both clients share a memoryblock where messages are read and written. You
          might need of course some sort of locking system. When recieving the
          message, the script tries to set it's semaphore in the memoryblock, then
          test if it got it, then write to it, then release the semaphore and raise a
          flag indicating that there is a new message waiting for the other
          participant.

          Both connections should be kept alive all the time, sending empty spaces
          every now and then should keep it alive. Time-outing must be disabled of
          course. I think it could work. It would be fun to develope. I've never used
          shmop thou, so I might be wrong, but that's the closest thing php has to
          "threads" AFAIK.

          --
          "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
          http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
          spam@outolempi. net || Gedoon-S @ IRCnet || rot13(xvzzb@bhg byrzcv.arg)


          Comment

          • Jerry Stuckle

            #6
            Re: PHP &amp; open connections

            Kimmo Laine wrote:
            "Lenard Redwood" <freakingcrazy@ gmail.comwrote in message
            news:1159809523 .767613.115970@ m73g2000cwd.goo glegroups.com.. .
            >
            >>Jerry Stuckle wrote:
            >>
            >>>You can't. HTTP is a pull only protocol. You can use javascript on the
            >>>client to pull the info on a regular basis (i.e. every 30 seconds). But
            >>>you can't push the data out.
            >>
            >>Exactly, actually each time user A writes a message and hits "Enter" it
            >>is sent right away to the server. But how can this message be thrown
            >>into the open connection that user B is using?
            >>Thanks,
            >
            >
            >
            Shared Memory Functions

            >
            Both clients share a memoryblock where messages are read and written. You
            might need of course some sort of locking system. When recieving the
            message, the script tries to set it's semaphore in the memoryblock, then
            test if it got it, then write to it, then release the semaphore and raise a
            flag indicating that there is a new message waiting for the other
            participant.
            >
            Both connections should be kept alive all the time, sending empty spaces
            every now and then should keep it alive. Time-outing must be disabled of
            course. I think it could work. It would be fun to develope. I've never used
            shmop thou, so I might be wrong, but that's the closest thing php has to
            "threads" AFAIK.
            >
            Shared memory isn't the problem. The problem is you cannot keep the
            connection open.

            Sending spaces will keep it open for a bit - but that means keeping your
            php code running all the time. And you can't do that unless you set the
            timeout parameter to a very large value. And that's not a good idea -
            what happens if another script starts looping, for instance.

            Even then the browser and/or server are free to close the connection if
            they feel it's been open too long, and most browsers will have a limit
            as to how long they will wait for data (even if some is still coming
            down the pipe).

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • Lenard Redwood

              #7
              Re: PHP &amp; open connections

              How about keeping the connection open for a minute, then reopen it?

              Services like Gtalk (http://mail.google.com/mail/help/chat.html),
              Interaction (http://www.interactionchat.com/) and Meebo
              (http://www.meebo.com/) use an open-connection and this allows
              reception of new messages instantly without the need of a Java applet
              or a Flash application. There's gotta be a way...

              Comment

              • Jerry Stuckle

                #8
                Re: PHP &amp; open connections

                Lenard Redwood wrote:
                How about keeping the connection open for a minute, then reopen it?
                >
                Services like Gtalk (http://mail.google.com/mail/help/chat.html),
                Interaction (http://www.interactionchat.com/) and Meebo
                (http://www.meebo.com/) use an open-connection and this allows
                reception of new messages instantly without the need of a Java applet
                or a Flash application. There's gotta be a way...
                >
                I don't know about Gtalk - I don't do Gmail, so I don't know how they do it.

                Interactionchat and Meebo both use Ajax, so I suspect they're using
                javascript to poll the server.

                No, there is absolutely no way - nada - zip - zero to the infinite power
                - way to "push" data with the http protocol. It is strictly a
                request/response protocol, with the request initiated by the client.


                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstucklex@attgl obal.net
                =============== ===

                Comment

                Working...