can a regular webhost and a separate node js server communicate? - php

I'm currently working on a trade / social network site as a side/learning/fun project.
At the moment I have an sql database messaging system. I want to create a live chat system like facebook which can tell the user is online, typing a message and everything.
I did some research into this and figured I need to start learning to use node.js and websockets. I called my webhost and they said they don't support node.js. I like the tools I'm using and do not want to migrate my website.
I also looked into some info about CORS.
I was thinking of a solution: client as well as the php server also communicates with an external node.js server using jquery to handle all chat and notification requests. The node.js server will also store messages in the php servers mysql database.
Is this solution feasible and am I on the right track? Or is there a better way?

Related

PHP websocket realtime updates on multiple devices

I am trying to implement a very simple self-hosted (docker) pubsub client update system using websockets or SSE for my PHP application.
I would like that a specific javascript action on my website triggers a php server-side update (via ajax) which should then lead to an update to all subscribed devices for the particular channel/topic. The subscribed devices should use javascript listening to one channel each.
Do you know any simple PHP ws libraries which are capable of that? Can SSE be triggered in that way and also utilize channels? How about in-built PHP socket function? Ideally I would like to stick to PHP solution in order not to deal with translations and in-between REST APIs.
After researching this for days and going through solutions like RabbitMQ, Redis, Kafka, RatchetPHP, ZeroMQ, socket.io, RPC, SSE and etc I am completely lost. I need some guidance on which way to go.
Among all possible solutions here is how I would do it:
Install a web socket server, preferably implemented in PHP, on you Linux system.
Your web clients register with the server and listen for messages like broadcast from the server.
Your ajax script registers also with the server and sends a broadcast to the server if all work is done.
The server passes the broadcast to all registered clients.
You could use my implementation of such a web socket server that comes with all the needed client
libraries for JavaScript as well as for PHP that implement broadcast and feedback.
See https://github.com/napengam/phpWebSocketServer

Is this the right choice for realtime notifications? php + Ratchet (websockets)

I'm new to websocket and realtime applications and I would need some clarifications.
I developed an application for a customer that involve some users and product data.
It is a PHP web application running on the internal apache server.
The client side of the app is based mainly on JQuery, and for some parts usable from tablet devices we used JQuery mobile / html5.
Now the customer is asking to add some messaging/notification feature in this way:
a user will be able to post a message about a product and the web console of the other users must be "showed" a realtime notification. We are thinking to add something for example in the header of the page to notify that new messages are present.
I made some research and as all the app is already built in PHP I was thinking to go for Ratchet. Is this a good idea?
If I understood this well, I will need to install on the server also the ratchet server components and use for client side integration something like AutobahnJS library; is it right?
Has anyone already built something like this?
Is there any problem communicating between Apache and Ratchet server?
That okie,
I tried use Ratchet/AutobahnJs Websocket, that's awesome. But I have (special?) problem :o in here.
In your case, I think you can use AJAX, it's simpler than Websocket. You can "zip" multi-notifications in to 1 response and request server every 5 seconds.
Yeah it's a good way to go about it, ajax leaves you with a lot of overheads. it's pretty simple to get going with ratchet and it will give you the ability to send notifications in real time, as well as the notifications it will allow you to edit the page as well, i.e change counters/ icons / message count etc, basically same as ajax only in realtime and no need to poll for new data.
It also means you'll be learning some new stuff which is always a bonus, for this alone i would do it.

Does drupal or joomla allow a combination node.js and socket.io with php?

I'm looking into the development of a site where socket use is a must for real time updates. I understand with wordpress it is difficult to incorporate node.js and socket.io and was wondering if this is the case with drupal. Specifically could I incorporate node.js and socket.io with php? What specific steps would I need to take? I appreciate the help and of course if you give me a good answer, I'll rate you up.
There are currently Node.js modules that connect to Drupal, Joomla, and WordPress. I wrote the one for Joomla: https://github.com/jlleblanc/nodejs-joomla You can find all of these through NPM. First, you would download one of these modules, then you would also download the Socket.io module. Then you would write server side code connecting the CMS to Socket.io. Finally, you would write client-side code to talk to the socket running in Node.
You can have it separated and since you want some client-server communication with node.js, you can have it - outside Drupal (this is the easiest and cleanest way).
CMS you use (be it Drupal or Wordpress) does not limit your JavaScript from using socket.io and node.js for the calls you want to be made outside the CMS server-side code. You can eg. read the same database Drupal/Wordpress does, but using Node.js and returning the results directly to the client-side JavaScript script.
You can integrate Socket.IO with whatever technology you like, but you need to think of them as 2 separate parts that need to communicate.
I see 2 solutions for Socket.IO to communicate with an external service (Drupal, Joomla, Wordpress, whatever):
1) Making a rest API for your Socket.IO application (for example using Express for the API layer and then passing messages to Socket.IO with an EventEmitter).
2) The better solution in my opinion is to use a message queue like Redis, RabbitMQ or ZeroMQ. The Drupal website would send a message down the channel to Socket.IO and then Socket.IO would send that message to the client(s).
If you are creating a system that needs authentication also, I suggest you don't let your users send message directly with Socket.IO, but make an Ajax call to your main server instead (Drupal for ex.). That way you can check the user's identity more easily. (This scenario would be a fit for a chat based system that requires authentication)
Although this video tutorial is for EventMachine with Faye, the same logic could apply to your app: http://railscasts.com/episodes/260-messaging-with-faye

Chat room app on Android without openfire

I plan to use asmack lib for a chatroom app on android, however, I just want to use PHP as server scripts with mysql as backend database server. I hope I can find a way to build it without openfire, is it possible? Any suggestions to implement them?
You can use whatever server you want. asmack is a client library that communicates via XMPP, which is language agnostic. Select any server you would like and your client should work fine.
Your comments about whether OpenFire has a PHP api though are somewhat confusing, do you want an OpenFire server or not? The language the server is written in is irrelevant to your client app.

Is it possible to run XMPP based PHP and Javascript Multiplayer Network Game in FaceBook?

let me introduce my story first. To develop a multiplayer network game in facebook, flash used to be a king but it consumes a lot of resources for client and I feel like it isn't worth for a card game. So I come with an idea that front-end will use Javascript (of course with jQuery) and backend with PHP. But for real-time communication, it isn't possible just with PHP and Javascript. It doesn't make sense to record every movement of players in MySQL and display back to another browsers. So I come with an idea with XMPP Services. XMPP services can even communicate browser to browser and display contents with Strophe and Javascript even without with PHP.
Finally, I've got every tools I need but I have few questions that I can't answer myself.
1) How XMPP server work between PHP and Javascript? I need to get/post user records from MySQL and calculate movements then forward results to intended user. There will be an Authentication system too so that's gonna be taken care by PHP as I'm not wrong.
2) This question is the title of this post.. Is it possible to run XMPP based PHP and Javascript Multiplayer Network Game in FaceBook?
Thanks in advance for all of your time and advices!
The connection would work using PHP sockets, and you could easily use Ajax to send data to PHP to send on to the XMPP server. There are also a number of libraries for communicating with XMPP servers using PHP. As for Facebook, it shouldn't have any effect on your script - if I remember correctly Facebook runs your app in its own sandbox so you shouldn't have to do anything special to get around the Facebook API.

Categories