Read More
Read More
Menu Close

    How To Use Socket.Io Programming With Opencart-Part2

    In our previous blog Link, we learned how to configure the node with socket.io in Opencart. Today we will continue about how to use socket.io scripting programming in a chat application. We will learn how to send or receive messages through socket.io functions.

    For the chat application, we have to manage two files:

    1) Server File

    2) Client File

    In the previous blog, we create a package.json file for defining the dependencies that we required for our application, you have to create a server.js file parallel to package.json file to your Opencart root directory:

    Searching for an experienced
    Opencart Company ?
    Read More

    File Structure

    1) server.js

    In server.js file we have to include all necessary dependencies at the top. We will also define the Global Configuration setting in server.js.

    Include Dependencies

    Emit Message back to sender

    In the above screenshot, socket.emit () used to response back to the sender. You can also display on the terminal on connection or on disconnect with the server. See the below screenshot, you can console the message on disconnection. If you will refresh the page, then you will see the “You are disconnected!” on the terminal.

    Disconnect From Server

    Now we will create the client.html file for the user to send the message. Firstly, we have to load a client library (socket.io.js) on browser for communication.

    <script src=”http://localhost:80/socket.io/socket.io.js“></script>

    Client File

    var socket = socket.io(http://[host]:[port]); 

    With the help of socket.io(http://) we can create an instance of socket.io by passing http object as an argument.

    Now go to terminal and execute this below mentioned command to run the server.

    $ node server.js

    After this send the message from user end. Screenshot_21

     

    On server.js we will receive the user message and will console on terminal.

    Screenshot_20

    Thanks for reading this blog 🙂

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top