Sending message only to particular websocket peer - php

I am working on a chat website and running php socket server ( using standard php sockets, based on script from https://github.com/sanwebe/Chat-Using-WebSocket-and-PHP-Socket/blob/master/server.php )
its all works fine, but I want to do some extra bits and not sure how it can be done (if can at all)
So the questions are:
Can I authenticate users trying to connect to websocket without them actualy creating connection and then sending login/authentication info via socket message?
How can I identify conneted peers (know which user is connecting to which socket) without them sending some info via socket message.
Can I send messages only to a particular peer connected to websocket server (That would be a main question)?
Thanks

Related

How to establish a persistent connection to a WebSocket server in Laravel to receive and send messages?

I have created a Laravel backend, which is an API. React frontend interacts with it. The frontend also communicates with the websocket server, receiving messages from it and reacting appropriately from it.
I was given the task of sending messages to a remote web socket from my backend.
I created the defined events to dispatch and listeners for those events. As far as I understand, in listeners I have to send messages to the websocket server. I was trying to use the textalk/websocket library, i.e. creating a websocket connection every time I sent a message to the websocket. It's wrong, but at least it worked. But now I need to keep a permanent connection with this web socket in order to receive messages from it and respond to them. And also I need to still keep sending messages on certain backend events.In general, I need to keep open the connection of my backend with a remote web socket server in order to constantly receive messages from it and in turn send messages to it at certain events within the Laravel application.

Best way to connect two socket servers (node.js and ratchet php)

There are two socket servers that have to send messages to each other. The first one is a node.js server with socket.io. The second is ja php websocket (ratchet - http://socketo.me/).
The second server (ratchet) runs in a php application (created with a php framework like laravel or symfony) that contains a basic chat application.
The node.js server is like a global manager that can convey messages to multiple socket servers.
The communication ways are planned as follows:
Scenario a)
-> client / browser sends a message via websocket
-> php app receives message from browser by ratchet socket and stores this message in database
-> php sends / forwards message to node.js via socket
-> php sends response to client / browser via websocket
Scenario b)
-> node.js server sends a message to all connected php clients via socket
-> php receives message from node.js and stores this message in database
-> php sends this message to one or more connected browser clients
-> browser receives messages from php via websocket
What is the best way to enable this communication way? The browser is already able to talk to the ratchet server. But I have no idea how the ratchet server can talk to the node.js server an act as a normal socket client so that they will establish and hold a connection to each other. Is there a command to send a “hello, I am a new client that wants to establish a connection” message to the node.js server or vice versa?
Is this a good concept at all? Or is there a better way to send socket messages over multiple instances? Are there any tips, tutorials or examples? Other questions to this topic are very old or contains a different setup.
I'd probably use this : https://github.com/ratchetphp/Pawl to have PHP sending messages to nodejs.
Then about the concept being good, I think not.
There is no point in having two differents websockets servers imo.
I'd probably use only one websockets server (nodeJS), and have PHP and node "talk" together through a messaging queue like rabbitMQ / REDIS

load balancing and socket.js

Currently I'm working on a project is about chat system between client and agent by using socket.js and PHP.
Recently, received confirmation from client is client server will have load balancer to balance to different server.
Like picture below.
I'm found out an issue from this setup.
Agent A is connected to Server B, but Client A is connected to Server A, so Client A can't communicate with Agent A.
How can I solve this problem by using programming way?
PS:
Client is not allow me to edit any server config file or settings.
Client server is linux server.
Thanks for help :)
we have a similar problem. Right now we have a solution, but it is most likely not the best. When we start our 2 socket servers on 2 different servers, then each server also creates a socketconnection to the other socket server. So each socket server is also a client of the other socket server. When Socket Server A receives a communication it sends it to all connected users AND it also broadcasts it to Socket Server B, and that one sends it then to all connected users again.
Another solution is a Publish/Subscribe broker, like you can find in that link here https://hackernoon.com/scaling-websockets-9a31497af051.
Hope that helps somehow.

Php Notification system and websocket

i'm trying to develop a PHP notification service for a CRM used by my customers.
Scenario: an user add an appointment for a collegue, and this collegue has to receive a notification inside his CRM profile like, for example, Facebook did.
The structure is pretty simple: a PHP Server in listening with the client browser connected with Websockets.
Server: i'm using Ratchet - PHP WebSockets, i've followed the examples and started the server, server that is able now to accept connection and deliver messages to all clients connected or just to one receiver.
Client: a simple JS script, with Websocket connection and method, that handle all receiving messagges and notify all to the user connected.
My point is: i'm guessing to have the websocket client browser just for incoming messagge, i don't want to allow them to send nothing, the send part must be done by the server that has access to DB in this way:
1) FORM Appointment
2) Submit to the server and save it to DB, in this step a client PHP Websocket connect to the Ratchet server and send the notification to the receiver.
3) The Websocket of receiver receive the notification and alarm the customer.
The questions are:
what can i use to do the step 2 with the PHP websocket client to connect with the Websocket server? is good to use Pusher interface like this example suggest: http://socketo.me/docs/push or can i avoid using this system?
how can i handle the idle time of websocket client ? a timer that send a ping like message to client for avoiding the dead of websocket?
-how handle possible down of client side or server side?
THanks to all of you!

PHP script that enables client communication with custom hardware

We have a wireless device (WIFI enabled) that calls a PHP script to enter data into MySQL db. Am trying to find a way to see if the server (through PHP script) can "connect" to the client device (has a microcontroller but doesn't have an OS or display) and pass some data.
The client is a electronic board connected to a gate and the server needs to tell the device to open the gate. Seems to be pretty straight forward but am new to this.
If the client is configured to listen on a specific port, you can open a socket connection to it from the server (using fsockopen()).
You'll need to configure the client though, which I have no idea how to do because your client is nothing standard.

Categories