Reader small image

You're reading from  Practical Python Programming for IoT

Product typeBook
Published inNov 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781838982461
Edition1st Edition
Languages
Right arrow

Socket.IO connect and disconnect handlers

In the <script> section of the file, on line (2), we create an instance of the io() class from the socket.io JavaScript library and assign it to the socket variable:

    var socket = io();                         // (2)

socket.on('connect', function() { // (3)
console.log("Connected to Server");
$("#connected").html("Yes");
});

socket.on('disconnect', function() { // (4)
console.log("Disconnected from the Server");
$("#connected").html("No");
});

On line (3), with socket.on('connect', ...), we register a connect event listener. This handler is called every time our web page client connects successfully to our Python server. This is the client-side equivalent of the Python server's on connect handler we defined with @socketio.on('connect').

On...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Practical Python Programming for IoT
Published in: Nov 2020Publisher: PacktISBN-13: 9781838982461