简体   繁体   中英

Client to client socket with php

How to make php client to client, like the chat way? One client connects and sends something to the other client and only he recieve not all clients.

Your Question?

If I understand correctly you want one-to-one(private) messaging.

Socket Programming using PHP

You need to learn Socket programming with PHP . You could start by studying this tutorial . This has scaling problems written all over it, because PHP does not have non blocking IO, proper thread model. I advise you to just use it for fun little projects.

Non blocking IO using PHP

You could try and use PHP-MIO . I have not yet tried this, but I guess it might scale. But then again from Apache(PHP) side you will have the same problems. But when using this from both sides it could work...

long-polling(blocking IO) using PHP

PS: got bored so I have not completely tested this;)

Download

Below I present two solutions(prototypes) which do NOT scale. One solution uses Redis pubsub . For this you need to install(compile) redis . For this you a POSIX OS is desired, although some people have ported it to Windows . You can also use the free redistogo.com instance. The Redis solution is the prefered solution. I have put everything in an archive which you can download from here .

I also give a solution which uses named pipes. This solution does not require you to use Redis, but instead this approach needs access to the file system. I believe that this approach should also work on Windows(You have to change the filename to windows-style). I would like for somebody to try this out:). I can not test this anymore, because I have completely switched to POSIX OS(Ubuntu) a long time ago.

Requirements

At least PHP 5.3 and preferable a POSIX OS, redis .

How to use

To use both solutions you need to open two browsers(Browser A / B ). I assume you are using localhost for development and that you can access files from http://localhost/6646733 .

  1. point browser A to http://localhost/6646733/redis?me=somebodyelse&to=alfred you should replace redis with pipe when trying out named pipes.
  2. Point browser B to http://localhost/6646733/redis?me=alfred&to=somebodyelse
  3. In browser A type a message into the textarea, which will be sent to browser B .
  4. In browser B read the message just sent from browser A

Solutions not using PHP

The solutions below scale.

Pusher(Hosted)

With for example the hosted solution Pusher you can do chat/messaging without the scaling nightmare. Pusher even is generous to provide a free plan. But be aware that the cheap plans do NOT offer SSL so the messages can be intercepted. You should never sent private information over the wire, when not using SSL. Users/developers have provided a nice little library to use Pusher from PHP . The problem with this approach is that you are not in control, but pusher is, but then again you don't have to worry about any details.

Socket.io(open-source)

I really like socket.io , but there are off course a lot of other solutions like for example tornado . You could use Redis to efficiently communicate between PHP and the other solution(socket.io).

I don't fully understand what you are trying to do, but you can use some kind of database and have it store messages that is sent to each user, and then have your client page refresh the chat part with a AJAX kind of query to update the chat. It will then behave simular to the new Facebook chat, where all messages are stored even the ones sent in normal chat and mail. So clients can mail and chat each other at all times, when they are online it will show in their chat and when thy are offline it will show up in their inbox. But this might not be what you are trying to do.

For implementing best Chat application, use jabber server and write clients using js/flex

http://en.wikipedia.org/wiki/Extensible_Messaging_and_Presence_Protocol

If it's not like chat but you want to send messages over without a server, you need both clients be server as well. A server will listen on a port for incomming connections. Write a daemon that spawns a new thread each time a client connects. Within this thread you handle the messaging.

Client A opens a connection to the server (Client B) and they can talk to each other. Or you let Client A become server and let Client B connect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM