Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

broadcast-messages-with-your-php-app-boom- #22

Open
rodriguesfas opened this issue Sep 2, 2021 · 9 comments
Open

broadcast-messages-with-your-php-app-boom- #22

rodriguesfas opened this issue Sep 2, 2021 · 9 comments

Comments

@rodriguesfas
Copy link

Hi!
Could you give me more details on how to use this piece of code: https://github.com/takielias/codeigniter-websocket#broadcast-messages-with-your-php-app-boom-

Please, if it's not too much bother, put an example of use. Hug.

@takielias
Copy link
Owner

@rodriguesfas can you please try this ?

@rodriguesfas
Copy link
Author

Thanks for answering me.

This link you sent me, I have already tested the application and it works very well.

However, it sends the message from ws in the frontend. I'm needing to send the message inside the php controller.

I'm trying to use Broadcast messages with your php App that you suggested in the documentation, but it doesn't work for me, I was wondering if you have a working example of this part or if you would put more details on how it works.

@rodriguesfas
Copy link
Author

Captura de tela de 2021-10-07 09-20-27

I'm trying to run that example of yours, but to no avail.

@JacoSJKruger
Copy link

JacoSJKruger commented Dec 21, 2021

Captura de tela de 2021-10-07 09-20-27

I'm trying to run that example of yours, but to no avail.

I'm experiencing same issue. Did you manage to solve?

I even changed the 0.0.0.0 to localhost (like: "ws://localhost:8282") but still no success.

@takielias
Copy link
Owner

@rodriguesfas @JacoSJKruger

Please look at the Welcome.php controller.

public function index()
{
	// Load package path
	$this->load->add_package_path(FCPATH . 'vendor/takielias/codeigniter-websocket');
	$this->load->library('Codeigniter_websocket');
	$this->load->remove_package_path(FCPATH . 'vendor/takielias/codeigniter-websocket');

	// Run server
	$this->codeigniter_websocket->set_callback('auth', array($this, '_auth'));
	$this->codeigniter_websocket->set_callback('event', array($this, '_event'));
	$this->codeigniter_websocket->set_callback('roomleave', array($this, '_roomleave'));
	$this->codeigniter_websocket->run();
}

public function _roomleave($data = null)
{
	// Here you will receive data from the frontend roomleave event trigger.
	echo 'Hey ! I\'m a room leave EVENT callback' . PHP_EOL;
}

The main concept is the callback function.

You would receive the response into the defined function. You can trigger the event from the front end like below using jQuery

    socket.send(JSON.stringify({
        'type': 'roomleave',
        'room_name': targetName,
        'user_id': "buzz4rd"
    }));

It would trigger the function below

public function _roomleave($data = null)
{
	// Here you will receive data from fron tend roomleave event trigger.
	echo 'Hey ! I\'m a room leave EVENT callback' . PHP_EOL;
}

Please look at this Room Chat using PHP websocket. It was built using this

@JacoSJKruger
Copy link

JacoSJKruger commented Jan 3, 2022 via email

@rodriguesfas
Copy link
Author

@rodriguesfas @JacoSJKruger

Please look at the Welcome.php controller.

public function index()
{
	// Load package path
	$this->load->add_package_path(FCPATH . 'vendor/takielias/codeigniter-websocket');
	$this->load->library('Codeigniter_websocket');
	$this->load->remove_package_path(FCPATH . 'vendor/takielias/codeigniter-websocket');

	// Run server
	$this->codeigniter_websocket->set_callback('auth', array($this, '_auth'));
	$this->codeigniter_websocket->set_callback('event', array($this, '_event'));
	$this->codeigniter_websocket->set_callback('roomleave', array($this, '_roomleave'));
	$this->codeigniter_websocket->run();
}

public function _roomleave($data = null)
{
	// Here you will receive data from the frontend roomleave event trigger.
	echo 'Hey ! I\'m a room leave EVENT callback' . PHP_EOL;
}

The main concept is the callback function.

You would receive the response into the defined function. You can trigger the event from the front end like below using jQuery

    socket.send(JSON.stringify({
        'type': 'roomleave',
        'room_name': targetName,
        'user_id': "buzz4rd"
    }));

It would trigger the function below

public function _roomleave($data = null)
{
	// Here you will receive data from fron tend roomleave event trigger.
	echo 'Hey ! I\'m a room leave EVENT callback' . PHP_EOL;
}

Please look at this Room Chat using PHP websocket. It was built using this

@JacoSJKruger

Hi, thanks for the explanation. Maybe I didn't explain well what I'm trying to do, I have two PHP applications, I want to send data from one to the other, but I don't want it through the front end, but backend to backend.

Thank you in advance for your attention in answering me.

@takielias
Copy link
Owner

Hi there, Thanks your suggestions did work indeed. I have another question though. Using php, how can I check which clients are currently active? My problem: What is a specific user has multiple open tabs of the same page, then each tab has the same client id. This causes glitches, so technically each page needs it’s own client id. However, I need to broadcast to all of these tabs for that user… This means I need to know which client id’s are active. Kind Regards, Jaco Jaco SJ Krüger B.Eng Mechanical Engineering Performance Manager Mobile: +27 79 361 0853 Website: www.basecloudglobal.com<http://www.basecloudglobal.com/> [A close up of a logo Description automatically generated]https://www.basecloudglobal.com/ [review-us-google]https://www.google.com/search?authuser=1&source=hp&ei=Ev9bXcDtNtDVwQLyxa64Dw&q=basecloud&oq=basecloud&gs_l=psy-ab.3..0i10l4j0i10i30j0i30j0i10i30l2j0i5i30l2.1561.2806..3059...1.0..0.435.2824.3-7j1......0....1..gws-wiz.....2..35i39j0i131j0j0i67.u-O_luTJYQQ&ved=0ahUKEwjAvviG0JHkAhXQalAKHfKiC_cQ4dUDCAU&uact=5#lrd=0x1e955fa6b3d0fdb5:0x5dcd69d9d6690d4a,1,,, From: Taki Elias @.> Sent: Tuesday, 21 December 2021 15:01 To: takielias/codeigniter-websocket @.> Cc: Jaco SJ Krüger @.>; Mention @.> Subject: Re: [takielias/codeigniter-websocket] broadcast-messages-with-your-php-app-boom- (#22) @rodriguesfashttps://github.com/rodriguesfas @JacoSJKrugerhttps://github.com/JacoSJKruger Please look at the Welcome.php controller. public function index() { // Load package path $this->load->add_package_path(FCPATH . 'vendor/takielias/codeigniter-websocket'); $this->load->library('Codeigniter_websocket'); $this->load->remove_package_path(FCPATH . 'vendor/takielias/codeigniter-websocket'); // Run server $this->codeigniter_websocket->set_callback('auth', array($this, '_auth')); $this->codeigniter_websocket->set_callback('event', array($this, '_event')); $this->codeigniter_websocket->set_callback('roomleave', array($this, '_roomleave')); $this->codeigniter_websocket->run(); } public function _roomleave($data = null) { // Here you will receive data from the frontend roomleave event trigger. echo 'Hey ! I'm a room leave EVENT callback' . PHP_EOL; } The main concept is the callback function. You would receive the response into the defined function. You can trigger the event from the front end like below using jQuery socket.send(JSON.stringify({ 'type': 'roomleave', 'room_name': targetName, 'user_id': "buzz4rd" })); It would trigger the function below public function _roomleave($data = null) { // Here you will receive data from fron tend roomleave event trigger. echo 'Hey ! I'm a room leave EVENT callback' . PHP_EOL; } Please look at this Room Chat using PHP websockethttps://github.com/takielias/ci-socket-chat. It was built using thishttps://github.com/romainrg/ratchet_client — Reply to this email directly, view it on GitHub<#22 (comment)>, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AWMUY3JV3KUXE7A2G77GE7LUSB3ALANCNFSM5DI2VH6Q. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you were mentioned.Message ID: @.@.>>

Define a global array of logged-in users. If you get a new logged-in user in that array it means already logged in. Disconnect the previous one and active the new one. Simple solution.

@takielias
Copy link
Owner

@rodriguesfas @JacoSJKruger
Please look at the Welcome.php controller.

public function index()
{
	// Load package path
	$this->load->add_package_path(FCPATH . 'vendor/takielias/codeigniter-websocket');
	$this->load->library('Codeigniter_websocket');
	$this->load->remove_package_path(FCPATH . 'vendor/takielias/codeigniter-websocket');

	// Run server
	$this->codeigniter_websocket->set_callback('auth', array($this, '_auth'));
	$this->codeigniter_websocket->set_callback('event', array($this, '_event'));
	$this->codeigniter_websocket->set_callback('roomleave', array($this, '_roomleave'));
	$this->codeigniter_websocket->run();
}

public function _roomleave($data = null)
{
	// Here you will receive data from the frontend roomleave event trigger.
	echo 'Hey ! I\'m a room leave EVENT callback' . PHP_EOL;
}

The main concept is the callback function.
You would receive the response into the defined function. You can trigger the event from the front end like below using jQuery

    socket.send(JSON.stringify({
        'type': 'roomleave',
        'room_name': targetName,
        'user_id': "buzz4rd"
    }));

It would trigger the function below

public function _roomleave($data = null)
{
	// Here you will receive data from fron tend roomleave event trigger.
	echo 'Hey ! I\'m a room leave EVENT callback' . PHP_EOL;
}

Please look at this Room Chat using PHP websocket. It was built using this

@JacoSJKruger

Hi, thanks for the explanation. Maybe I didn't explain well what I'm trying to do, I have two PHP applications, I want to send data from one to the other, but I don't want it through the front end, but backend to backend.

Thank you in advance for your attention in answering me.

Why don't you use rest API?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants