We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon and websockets

Hi

Since there is no real websocket implementation in phalcon I'm trying to use a very simple php websocket library that i wrote some time ago. But since i'm new to phalcon I find it hard to know how to correctly make the sockets available in phalcon using the correct methods.

I've been looking at the webird project, but that is more of a spin off framework of phalcon, it does use Ratchet (socket.io) but it is such a big implementation using sevral sub libraries (react).

My question is, how to register my classes (websocketCOnnect, websocket and websocketHelper (extends websocket) ) to the phalcon project using the supplied phalcon methodoligy.

I guess I can use a dispacher to register the service and use eventsManager to manage data and actions. But I don't know if this is the correct way to do it. Even if it is the correct way, beeing new to phalcon I'm not sure how to implement it. Is there any similar projects using some kind of streams(?) that i can take a look at, just to see how they are implemented the phalcon way? :)

Regards André

edited Dec '15

Why you even want to use websockets in php ? Its the worst thing you can do to be honest. Php is not for such a thing, use javascript(node). You can register them just by using loader, and then use them however you like.

Hi and thanks for your answer.

I wouldn't go as far as saying its the worst withng :), bur you are right, for most chat/board systems nodejs is a great choice. But for the current project i need a websocket server that that accepts UDP connections, and as far as i know javascript can not do that.

Also i like to keep things clean and I wouldn't personally mix different frameworks unless it is really necessary. :)

Loading the classes might be the trick. Any idea, how do broadcast the recieved message to the MVCs in the proper way?

Cheers André



145.0k
Accepted
answer
edited Dec '15

I think you would need cli phalcon application, phalcon works on HTTP and router. You would need to write some adapter ono php side which would take websocket message, create phalcon application and then somehow parse this message to some phalcon route and then you get resonse from phalcon and return broadcast this response to all clients/whatever.

Its just simpler to use node for all websocket communication and call php cli for all application logic if you still wanna use php.

Thank you @Jurigag.

The phalcon cli would accually work as I would like to. I'm going to try implementeing that.