Best way to connect two socket servers (node.js and ratchet php) - 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

Related

why do you need to use a third party server for websockets

I saw this answer why do I need a third-party server for websockets about using a third-party server for WebSocket but I didn't understand what the meaning of Laravel itself is not a WebSocket server and when working with a service like pusher then the connection between my server and pusher is through HTTP but the connection between pusher and client is through WebSocket
WebSocket server uses UDP to broadcast events to your clients, so you need infinity cycle where you handle incoming events from your server and clients.
Laravel however was designed for PHP - where new daemon created for each incoming request over HTTP (which is TCP). You may trigger some events in your app and throw them for your socket server by TCP, which then will broadcast it over UDP for all your clients.
This is not quite comfortable to mix things up in PHP like in Node.JS where you may have full access to web server in single process. That's why you need external process for WebSockets with Laravel. It may be the same server, but you're required to use UDP for sockets, that's all.
P.S.: The term "third-party server" in this context was probably caused because PHP apps in a lot of cases hosted on virtual hostings, not on a server directly. And usually hostings don't give you the ability to spawn long staying processes.

Sending message only to particular websocket peer

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

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!

How to Develop an Arbitrary Socket.IO Client that can Connect to a Socket.IO Server

I am trying to develop a socket.io application. It is just a simple application like the one on the Getting Started - Chat application tutorial in the Socket.IO webpage. But the problem is that, I do not want to have the socket.io server serve the client automatically, because I am planning to have a page (in PHP) have a script that can simultaneously be updated by data from a server in real-time using Socket.IO. I just want a certain client that can connect to the server and let them communicate, not a page served by the socket.io server.
Is this possible? Can you please give an idea on how can it be done. Thanks.
That chat tutorial is just a simple deployment where your own web page wants to connect back to the same server that the web page came from. That is not the only way to do it.
The socket.io client code can be served from anywhere. You can even link to a CDN and get it from there instead of from your own server.
And, likewise, the client can connect to any compatible server as long as the server is configured to accept a connection from your page.
If you're looking for how to connect to a different server, you just specify the URL of the desired host in io.connect() like this:
var socket = io.connect('http://www.example.com');

Making the web server as a relay between two sides

I want to control a robot from the web, the robot is connected to Android device. The operation will be as the following :
a web application written using JavaScript and HTML runs on desktop computer which takes the keyboard input from the user and send them to the android device connected to the robot.
the android device receive the commands and then send them to Arduino board which used to control the robot.
But how should I deliver the data to the Android device which doesn't has a static IP address?
I have two approaches to solve that :
the JavaScript application sends the keyboard input to a web server runs PHP and MySQL , then the php application store the data on the MySQL database. An application runs on Android connected to that web server and extract the data from the MySQL database.
the JavaScript sends the data to the web server. The android application connected to the web server receives the data directly so the web server is just used as a relay.
The first approach is easy to do but its slow , so my question is...
How to implement the second approach and which web technologies should I use to implement it? And how to make the web server works as relay between two sides?
PS : I am planning to use 000webhost.com as web server. so I will not use my own server
You can either have your Android application poll the webserver for outstanding commands. This is a little inefficient in terms of data usage, but if you're on an unlimited 3G plan / wi-fi, you could live with it. It will be very easy to implement.
Alternatively, set up a TCP server on your server, and have your Android application open a socket connection with the server. This way, your web application can send commands to the server which will immediately stream them to the Android device. It will be slightly harder to implement, but will be more efficient and robust if done right.
PS - Most shared servers don't allow you to open a TCP server on your host so you might be forced to go with the first option.
PPS - I wasn't aware of Google Cloud Messaging. It seems to be a good solution for you what you're attempting to achieve. You should have a look into it.

Categories