phpsocket.io icon indicating copy to clipboard operation
phpsocket.io copied to clipboard

How to send a message to a socket from different parts of the code?

Open Bezlepkin opened this issue 6 years ago • 6 comments

For example i have two php files. index.php, index2.php. And both should send to the socket.

` use Workerman\Worker; use PHPSocketIO\SocketIO;

// listen port 2021 for socket.io client $io = new SocketIO(2021); $io->on('connection', function($socket)use($io){ $socket->on('chat message', function($msg)use($io){ $io->emit('chat message', $msg); }); });

Worker::runAll(); ` Need to create an instance of SocketIO twice?

Bezlepkin avatar Oct 28 '19 07:10 Bezlepkin

Same here, how to do this?

DavyMassoneto avatar Feb 04 '20 18:02 DavyMassoneto

You import it from the main file e.g in index2.php

'index.php' ```
Then you can use it in as usual

devyaz avatar Feb 05 '20 12:02 devyaz

$io=require 'index.php'

devyaz avatar Feb 05 '20 12:02 devyaz

$io=require 'index.php'

Don't work

DavyMassoneto avatar Feb 21 '20 19:02 DavyMassoneto

What's the error you are getting?

On Fri, 21 Feb 2020 21:29 Davy Massoneto, [email protected] wrote:

$io=require 'index.php'

Don't work

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/walkor/phpsocket.io/issues/209?email_source=notifications&email_token=AE35ASMG7FQ7IL6GCPIHGJ3REATRLA5CNFSM4JFWPF4KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEMT2HDA#issuecomment-589800332, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE35ASPRQYNOVGYC7JG6B3DREATRLANCNFSM4JFWPF4A .

devyaz avatar Feb 27 '20 07:02 devyaz

"Need to create an instance of SocketIO twice?"

No, The first script/file should connect to the socket then you can use the Emitter to send from other PHP files or scripts.

See here https://github.com/walkor/phpsocket.io/issues/23

tolew1 avatar Aug 12 '20 19:08 tolew1