简体   繁体   中英

Is it possible to integrate socket.io client in Laravel?

I'm having an external application which will connect to my website using sockets (using zeromq ).

Those sockets will be received in node.js (express.js) and sent to the clients through socket.io .

The purpose of using socket.io is just to update some information on every client browser in real time. Just like simple notifications. No websockets will be transmitted from the browser to the server. The browser will use websockets only to receive information from the server.

I'm wondering if it would be possible to integrate it with Laravel so I can still using my laravel login, sessions, URL routes etc.

You may use next solution. I using it in PHP+socket.io connection. This is not full integration, but you may emit events from PHP code to connected clients.

1. Connect to socket.io in client.

2. Create userid in PHP (I use md5(session_id())). Save this id where you want, for example, in user accounts table.

3. Run next code in client side:

<script>
var socket = io("' . $your_socket_io_server . '");
socket.emit("register", { userid: "' .  $userid  . '" });
</script>

4. Listen events on client

<script>
    //Additional room, not inly user id.  
    socket.emit("register", { userid: "room_26322bab71bc91b19204acfeee6192" })
    socket.on('server_event', function (data) {
        console.log('server event');
        console.log(data);
    })

    socket.on('alert', function (data) {
        alert(data);
    })
</script>

5. Write some node.js code, which listen GET and POST queries and emitting events in correct userid client. You may create room named $userid for this.

6. Send you command to node.js server from PHP via file_get_contents or curl.

For example, my code: https://github.com/ramainen/doit-socket/blob/master/server.js

One room - one client. After disconnecting, room deleting, but now there is a bug in socket.io, and room don't deleting, so i delete in manually in code.

7. So, adress may be next: your-nodejs-server/emit?id=a98166839163afd20a4be56fa3e60d13&message=server_event&param=12

Also, better using curl with PHP data. So, all business-logic in PHP, you don't use data storage or calculating in node.js part, also you can use one node.js server for several projects. And you use your Laravel logins, sessions, whatever you want. Also, you may ose userid_to_user many-to-many storage in you PHP part for serve several clients, logged in on account.

You may use adress cloud.doit-cms.ru for testing, which is Azure node.js app, connecting to github code https://github.com/ramainen/doit-socket

My PHP code for emiiting (its pretty simple): https://github.com/ramainen/doit-cms/blob/master/cms/mod_socketio/socketio.class.php . Of course, external application (if it not in PHP) must use this simple API.

Also, you may use third-part solution, like this:

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