1

I am writing a webservice in Slim Framework. But that doesn't concern to the problem. what I am doing is in my webservice I am performing some db tasks and before returning the response in json, I am sending message using socket.io.

Below is my code:

$db = new DbHandler();
$response = $db->savehairPatient();
echoRespnse(200, $response);
 ?>
    <script src="../node_modules/socket.io/node_modules/socket.io-client/socket.io.js">
    </script>
    <script>

    history.pushState('', '', 'patient');
    var socket = io.connect('http://' + window.location.hostname + ':3000');

    socket.emit('new_count_message',
        {
            new_count_message: '2'
        });

    socket.emit('new_message',
        {
            name: "name",
            email: "email",
            subject: "23dfad",
            created_at: "12",
            id: pid
        });
    </script>
 <?php

When I checked the json response, it is giving me the same script written above instead of sending push.

kamranbhatti585
  • 232
  • 2
  • 16
  • 1
    So are you expecting the PHP runtime to run your JavaScript code? – tyteen4a03 Nov 14 '17 at 11:42
  • have you wrapped the code in `` and ensured its a `.php` file and that is being parsed by the `php` `bin` – atoms Nov 14 '17 at 11:42
  • @tyteen4a03 I know its an server and client side different languages. But what should I do then to achieve what I am doing? – kamranbhatti585 Nov 14 '17 at 11:43
  • @kamranbhatti585 Rewrite your JavaScript code in PHP. – tyteen4a03 Nov 14 '17 at 11:44
  • @atoms Yes. The actual problem is pointed by tyteen4a03 above. – kamranbhatti585 Nov 14 '17 at 11:45
  • 1
    I have no idea what you are trying to ask here. – Quentin Nov 14 '17 at 11:45
  • 1
    *When I checked the json response* — the JSON response … to what? Where are you examining this response? In the browser? In the server hosting the PHP? In the Node.js server? – Quentin Nov 14 '17 at 11:45
  • *instead of sending push* — What does "sending push" mean? The word "push" only appears in your call to `history.pushState`. – Quentin Nov 14 '17 at 11:46
  • *it is giving me the same script written above* — Do you mean the object hard coded into the call to `socket.emit`? Why wouldn't it give you that? It is hard coded. There is no JS or PHP that would change those values. (Except for `pid` which you don't seem to have defined at all so I would expect to see a ReferenceError) – Quentin Nov 14 '17 at 11:47
  • 1
    "The actual problem is pointed by tyteen4a03 above" — Why do you expect PHP to run that code? It is written so PHP will output it directly without processing (it is outside of ``) so it will be sent to the browser and run on the browser. – Quentin Nov 14 '17 at 11:48
  • 1
    @Quentin I want to send push using **script** code but as my webservice is written in **PHP** so I am not able to run **javascript** as it is a client side language. – kamranbhatti585 Nov 14 '17 at 11:50
  • JavaScript is not "a client-side language". It is a programming language that has client applications, server applications, and non-client-server applications. – Quentin Nov 14 '17 at 11:51
  • Is your question: "I want to use Socket.io, but it is a tool that requires Node.js which I am not using. What can I do?" – Quentin Nov 14 '17 at 11:52
  • @Quentin Simple Question: My socket push is written in Javascript, and I want to run this inside php webservice. But as javascript is only executed by browser so what can I do to run my code? Is there way to write the same code in PHP? If there is any, then kindly give me the reference. – kamranbhatti585 Nov 14 '17 at 11:52
  • The code you've provided is a mix of client side JavaScript and server side PHP. If there is some server side JS that you are asking about … then you should include it in the question. – Quentin Nov 14 '17 at 11:53
  • @Quentin No. I am able to send push if I run same PHP function by creating a new file outside webservice. – kamranbhatti585 Nov 14 '17 at 11:54
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158955/discussion-between-kamranbhatti585-and-quentin). – kamranbhatti585 Nov 14 '17 at 11:55
  • Every time you add more information I feel like I have less understanding of what the problem is. [This might be a duplicate](https://stackoverflow.com/questions/6398887/using-php-with-socket-io), but I'm not going to devote any more time to trying to understand you. – Quentin Nov 14 '17 at 11:56
  • So you're basically asking us to rewrite the JS code into PHP for you. What have you tried? – tyteen4a03 Nov 14 '17 at 13:58

0 Answers0