1

I am working on codeigniter rest api,and at the sametime i need to push some count message via socket, so i have written js code using echo (to run js code in php we need echo), but socket code is not working, i.e at receiver side I am not getting any message.

socket code in rest api file:

     if ($this->db->insert('users', $new_arr)) {
                    echo '<script src="' . base_url("node_modules/socket.io/node_modules/socket.io-client/socket.io.js") . '"></script>';
    echo " var socket = io.connect( 'http://'+window.location.hostname+':3000' )";// open connection at sender
                    echo 'socket.emit("new_count_message", {  // emit message
                      new_count_message: 145
                    })';
                    $response = array('status' => true, 'message' => 'data dded successfully', 'response' => array());
                } else {
                    $response = array('status' => true, 'message' => 'Failed to add data!');
                }
              $this->response($response);

At rceiver:

    var socket = io.connect( 'http://'+window.location.hostname+':3000' );// connection open at receiver
     socket.on( 'new_count_message', function( data ) {
     alert(data.new_count_message);
  
      $( "#new_count_message" ).html( data.new_count_message );
   $('#notif_audio')[0].play();

  });
6
  • It looks like both your receiver and your html rendered by the rest api file are trying to connect to a socket, but it doesn't look like you are opening a socket connection anywhere. You can follow the docs here socket.io/docs/v3/index.html to use sockets with an existing httpserver under "Minimal working example". Basically, the connection needs to be open as long as the server is running, and is separate from the webpage generated and returned from your API. Your rest api file is "emitting" to nothing, as it isn't connected to any active socket. Commented Dec 21, 2020 at 7:21
  • @sal I have opened connection please check my code, sender and receiver bth side connection is opened and I am running node ser.js file via cmd which is located in project directory Commented Dec 21, 2020 at 7:25
  • Where are you opening the connection? The rest api file looks like it's returning a webpage, and the io.connect() method inside of there is looking to connect to an open socket, not open one. If you're opening it somewhere else, please include that code as well, or correct me if I'm missing something. Commented Dec 21, 2020 at 7:31
  • @Sal I have updated my questionin same way i did in previous website (sending and receiving done from webpage) and in that case it worked fine, but here I am sending from api and try to receive at webpage Commented Dec 21, 2020 at 8:26
  • I don't have extensive experience with websockets, however anytime I've used socket.io (in node), I have always emitted a message from a client to a server, and then the server would emit the message to the appropriate clients. I'm not aware of any way to emit from one client directly to another without a server, which is what your code seems to be doing. Your API file doesn't seem to be serving as the server socket connection, it's just returning HTML so that the client can connect to a socket, but that open socket isn't in this code. Try github.com/walkor/phpsocket.io? Commented Dec 21, 2020 at 8:39

1 Answer 1

1

I haven't working with socket.io in php, however I came across this library https://github.com/walkor/phpsocket.io that allows you to implement socket.io in php. Following the "Simple chat" example should show you how you can set up the server-side code. What you have in your api file doesn't open a server-side connection, rather returns a website that emits data from the client side. Because you don't have a server handling socket connections, it is unable to transmit the data to other sockets. After configuring your server-side socket handler, when the API call is made and the code in your API file is called, the client will receive a webpage with the information you have outputted. In the received data, you are connecting to a socket and emitting data. That data will then be received by the server in the code you implemented following the example I linked, then emit the data to the connections you had set.

Alternatively, rather than making an API call in the first place, why don't you just emit a message instead? This will save you a network call (the API call is unnecessary if all it is doing is triggering the socket) and should make your application faster. It's hard to give exact implementation details without seeing how the API is being called in the first place.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.