简体   繁体   中英

Access NodeJS / Socket.io over PHP and cURL

I'm running a nodejs / socket.io (v 0.7) server to push data to web clients. That works greatly.

To extend the application, I need to trigger / influence socket.io over php. I know a solution that works with cURL over http with the nodejs http server.

One possible solution is to run the socket.io server and the nodejs http server in one an the same app.js. The clients connect and communicate with socket.io, and the only allowed connection to the "seperate" http server (listening on another port than socket.io, of course) is from 127.0.0.1 respectively from the php server over cURL.

Because the both servers are in the same scope, the http server can trigger events on the socket.io server.

My Question: doest that make any sense? Is there a cleaner solution to trigger events on socket.io from PHP?

I think that's a reasonable and straightforward way to go; it seems like caution would be advised for dealing with order of execution, but that's probably just my uncertainty with node showing.

I am writing a solution which I think looks a lot like pusher API. It utilizes room(channel) concept. From a route via express I can curl(via PHP) to send messages to room(channel).

io.sockets.in('your room').json.send('your json');

I'd like to open-source that project in the future(hopefully within a week), but hopefully you get the picture.

socket.io with version greater than 1.3.3 (may be older),you can trigger a emit event as below

//send broadcast message to all connecting clients
io.emit([message event],[your message body obj or str]);

// send to specific user
io.to([your room]).emit([message event], [your message body obj or str]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM