skip to Main Content

I’m kinda new on sockets related subject so I’m sorry for any dumb question.

I would like to do something like this… I have am hybrid app and a website, and I wanted that when I click in a button on the app, it shows me alert/notificaion on the website. I read about Socket io and it does the job on localhost, but I want na alternative that not uses a server behind, since I’m not being able to run it using CPANEL (What I have access to)
Is it possible to have like a “direct” connection from the app to the site when I click the button?

4

Answers


  1. You can write a PHP script that has a POST/GET endpoint. Your app will communicate to this endpoint. The endpoint needs to handle the message and write it to a database. Your website can then poll to see if there are any new entries, and show something if there are

    Login or Signup to reply.
  2. Let’s break the problem down into a few parts, starting with the transport to the browser, as that’s what you’re asking about.

    Web Sockets are a way to establish a bi-directional connection between a server and a client. It’s a standard implemented by most any modern browser. Socket.IO is a web-socket-like abstraction that can use Web Sockets or other transports under the hood. It was originally built as sort of a polyfill, allowing messages to be sent via Web Sockets, or even long-polling. Using Socket.IO doesn’t give you any additional capability than you have with just the browser, but it does provide some nice abstractions for “rooms” and such.

    If you’re sending data only from the server to the client, Web Sockets aren’t the ideal choice. For streaming of data in general, the Fetch API and ReadableStream are more appropriate. Then, you can just make a normal HTTP connection. However, what you’re looking for is event-style data, and for that there are Server-Sent Events (SSE). https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events Basically, you instantiate an EventSource object on the client, pointed at a URL on the server. The client automatically maintains a connection, reconnecting if necessary. It’s also capable of synchronizing to a point in the stream, providing the server with the last message received so that the client can be caught up to present time.

    Now, how does your server endpoint know when to send this data, and what to send? Ideally, you’ll use some sort of pub/sub system. These capabilities are built into Redis, which is commonly used for this. (There are others as well, if you don’t like Redis for some reason.) Basically, when your server receives something from the app, the app is “publishing” a message to a particular channel where all “subscribers” will receive it. Your server will be that EventSource and can simply relay data (verifying it and authenticating of course, along the way).

    Login or Signup to reply.
  3. You can consider using firebase for this:

    In your javascript:

    // execute the following script on click
    importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-app.js');
    importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-messaging.js');
    
    // Initialize the Firebase app in the service worker by passing in the
    // messagingSenderId.
    firebase.initializeApp({
      'messagingSenderId': 'YOUR-SENDER-ID'
    });
    
    // Retrieve an instance of Firebase Messaging so that it can handle background
    // messages.
    const messaging = firebase.messaging();
    messaging.send({data: "your data if you want to send"}).then((response) => {
        // Response is a message ID string.
        console.log('Successfully sent message:', response);
      })
      .catch((error) => {
        console.log('Error sending message:', error);
      });
    
    // similarly, on your browser:
    messaging.onMessage(function(payload) {
      console.log('Message received. ', payload);
      // ...
    });
    

    link: https://firebase.google.com/docs/

    Hope it helps

    Login or Signup to reply.
  4. Alright, let’s do it in PHP. This is just the most basic example. Just put it somewhere and link to the script from your app.

    <?php   
    
     function requestVars($type = 'REQUEST'){
    
       if($type == 'REQUEST')
          $r = $_REQUEST;
       elseif($type == 'POST')
          $r = $_POST;
       elseif($type == 'GET')
          $r = $_GET;
    
       $ret = array();
    
       foreach($r as $r1 => $r2)
         $ret[$r1] = $r2;
    
      return $ret;   
    }
    
    $vars = requestVars(); //get variables from request
    echo $vars['var1'];   // var1 is what comes in from the client
    
    ?>
    

    I haven’t tested this, so if something is wrong let me know.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search