Socket.IO – Rooms

Socket.IO – Rooms ”; Previous Next Within each namespace, you can also define arbitrary channels that sockets can join and leave. These channels are called rooms. Rooms are used to further-separate concerns. Rooms also share the same socket connection like namespaces. One thing to keep in mind while using rooms is that they can only be joined on the server side. Joining Rooms You can call the join method on the socket to subscribe the socket to a given channel/room. For example, let us create rooms called ”room-<room-number>” and join some clients. As soon as this room is full, create another room and join clients there. Note − We are currently doing this on the default namespace, i.e. ”/”. You can also implement this in custom namespaces in the same fashion. var app = require(”express”)(); var http = require(”http”).Server(app); var io = require(”socket.io”)(http); app.get(”/”, function(req, res){ res.sendfile(”index.html”); }); var roomno = 1; io.on(”connection”, function(socket){ socket.join(“room-“+roomno); //Send this event to everyone in the room. io.sockets.in(“room-“+roomno).emit(”connectToRoom”, “You are in room no. “+roomno); }) http.listen(3000, function(){ console.log(”listening on localhost:3000”); }); Just handle this connectToRoom event on the client. <!DOCTYPE html> <html> <head><title>Hello world</title></head> <script src=”/socket.io/socket.io.js”></script> <script> var socket = io(); socket.on(”connectToRoom”,function(data){ document.body.innerHTML = ””; document.write(data); }); </script> <body></body> </html> Now if you connect three clients, the first two will get the following message − You are in room no. 1 Leaving a Room To leave a room, you need to call the leave function just as you called the join function on the socket. For example − To leave room ”room-1”, socket.leave(“room-“+roomno); Print Page Previous Next Advertisements ”;

Sed – Pattern Range

Stream Editor – Pattern Range ”; Previous Next In the previous chapter, we learnt how SED handles an address range. This chapter covers how SED takes care of a pattern range. A pattern range can be a simple text or a complex regular expression. Let us take an example. The following example prints all the books of the author Paulo Coelho. [jerry]$ sed -n ”/Paulo/ p” books.txt On executing the above code, you get the following result: 3) The Alchemist, Paulo Coelho, 197 5) The Pilgrimage, Paulo Coelho, 288 In the above example, the SED operates on each line and prints only those lines that match the string Paulo. We can also combine a pattern range with an address range. The following example prints lines starting with the first match of Alchemist until the fifth line. [jerry]$ sed -n ”/Alchemist/, 5 p” books.txt On executing the above code, you get the following result: 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 We can use the Dollar($) character to print all the lines after finding the first occurrence of the pattern. The following example finds the first occurrence of the pattern The and immediately prints the remaining lines from the file [jerry]$ sed -n ”/The/,$ p” books.txt On executing the above code, you get the following result: 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 We can also specify more than one pattern ranges using the comma(,) operator. The following example prints all the lines that exist between the patterns Two and Pilgrimage. [jerry]$ sed -n ”/Two/, /Pilgrimage/ p” books.txt On executing the above code, you get the following result: 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 Additionally, we can use the plus(+) operator within a pattern range. The following example finds the first occurrence of the pattern Two and prints the next 4 lines after that. [jerry]$ sed -n ”/Two/, +4 p” books.txt On executing the above code, you get the following result: 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 We have supplied here only a few examples to get you acquainted with SED. You can always get to know more by trying a few examples on your own. Print Page Previous Next Advertisements ”;

Solidity – Discussion

Discuss Solidity ”; Previous Next Solidity is a contract-oriented, high-level programming language for implementing smart contracts. Solidity is highly influenced by C++, Python and JavaScript and has been designed to target the Ethereum Virtual Machine (EVM). Print Page Previous Next Advertisements ”;

Mathematical Functions

Solidity – Mathematical Functions ”; Previous Next Solidity provides inbuilt mathematical functions as well. Following are heavily used methods − addmod(uint x, uint y, uint k) returns (uint) − computes (x + y) % k where the addition is performed with arbitrary precision and does not wrap around at 2256. mulmod(uint x, uint y, uint k) returns (uint) − computes (x * y) % k where the addition is performed with arbitrary precision and does not wrap around at 2256. Following example shows the usage of mathematical functions in Solidity. Example pragma solidity ^0.5.0; contract Test { function callAddMod() public pure returns(uint){ return addmod(4, 5, 3); } function callMulMod() public pure returns(uint){ return mulmod(4, 5, 3); } } Run the above program using steps provided in Solidity First Application chapter. Click callAddMod button first and then callMulMod button to see the result. Output 0: uint256: 0 0: uint256: 2 Print Page Previous Next Advertisements ”;

Solidity – Error Handling

Solidity – Error Handling ”; Previous Next Solidity provides various functions for error handling. Generally when an error occurs, the state is reverted back to its original state. Other checks are to prevent unauthorized code access. Following are some of the important methods used in error handling − assert(bool condition) − In case condition is not met, this method call causes an invalid opcode and any changes done to state got reverted. This method is to be used for internal errors. require(bool condition) − In case condition is not met, this method call reverts to original state. – This method is to be used for errors in inputs or external components. require(bool condition, string memory message) − In case condition is not met, this method call reverts to original state. – This method is to be used for errors in inputs or external components. It provides an option to provide a custom message. revert() − This method aborts the execution and revert any changes done to the state. revert(string memory reason) − This method aborts the execution and revert any changes done to the state. It provides an option to provide a custom message. Example Try the following code to understand how error handling works in Solidity. pragma solidity ^0.5.0; contract Vendor { address public seller; modifier onlySeller() { require( msg.sender == seller, “Only seller can call this.” ); _; } function sell(uint amount) public payable onlySeller { if (amount > msg.value / 2 ether) revert(“Not enough Ether provided.”); // Perform the sell operation. } } When revert is called, it will return the hexadecimal data as followed. Output 0x08c379a0 // Function selector for Error(string) 0x0000000000000000000000000000000000000000000000000000000000000020 // Data offset 0x000000000000000000000000000000000000000000000000000000000000001a // String length 0x4e6f7420656e6f7567682045746865722070726f76696465642e000000000000 // String data Print Page Previous Next Advertisements ”;

Sed – Branches

Stream Editor – Branches ”; Previous Next Branches can be created using the t command. The t command jumps to the label only if the previous substitute command was successful. Let us take the same example as in the previous chapter, but instead of printing a single hyphen(-), now we print four hyphens. The following example illustrates the usage of the t command. [jerry]$ sed -n ” h;n;H;x s/n/, / :Loop /Paulo/s/^/-/ /—-/!t Loop p” books.txt When the above code is executed, it will produce the following result. A Storm of Swords, George R. R. Martin The Two Towers, J. R. R. Tolkien —-The Alchemist, Paulo Coelho The Fellowship of the Ring, J. R. R. Tolkien —-The Pilgrimage, Paulo Coelho A Game of Thrones, George R. R. Martin In the above example, the first two commands are self-explanatory. The third command defines a label Loop. The fourth command prepends hyphen(-) if the line contains the string “Paulo” and the t command repeats the procedure until there are four hyphens at the beginning of the line. To improve readability, each SED command is written on a separate line. Otherwise, we can write a one-liner SED as follows: [jerry]$ sed -n ”h;n;H;x; s/n/, /; :Loop;/Paulo/s/^/-/; /—-/!t Loop; p” books.txt When the above code is executed, it will produce the following result. A Storm of Swords, George R. R. Martin The Two Towers, J. R. R. Tolkien —-The Alchemist, Paulo Coelho The Fellowship of the Ring, J. R. R. Tolkien —-The Pilgrimage, Paulo Coelho A Game of Thrones, George R. R. Martin Print Page Previous Next Advertisements ”;

Solidity – Useful Resources

Solidity – Useful Resources ”; Previous Next The following resources contain additional information on Solidity. Please use them to get more in-depth knowledge on this. Useful Links on Solidity Solidity − Official Website of Solidity and Documentation Solidity Wiki − Wikipedia Reference for Solidity Remix IDE − On-line IDE for Solidity Compilation and Execution To enlist your site on this page, please drop an email to [email protected] Print Page Previous Next Advertisements ”;

Solidity – Events

Solidity – Events ”; Previous Next Event is an inheritable member of a contract. An event is emitted, it stores the arguments passed in transaction logs. These logs are stored on blockchain and are accessible using address of the contract till the contract is present on the blockchain. An event generated is not accessible from within contracts, not even the one which have created and emitted them. An event can be declared using event keyword. //Declare an Event event Deposit(address indexed _from, bytes32 indexed _id, uint _value); //Emit an event emit Deposit(msg.sender, _id, msg.value); Example Try the following code to understand how an event works in Solidity. First Create a contract and emit an event. pragma solidity ^0.5.0; contract Test { event Deposit(address indexed _from, bytes32 indexed _id, uint _value); function deposit(bytes32 _id) public payable { emit Deposit(msg.sender, _id, msg.value); } } Then access the contract”s event in JavaScript code. var abi = /* abi as generated using compiler */; var ClientReceipt = web3.eth.contract(abi); var clientReceiptContract = ClientReceipt.at(“0x1234…ab67” /* address */); var event = clientReceiptContract.Deposit(function(error, result) { if (!error)console.log(result); }); It should print details similar to as following − Output { “returnValues”: { “_from”: “0x1111…FFFFCCCC”, “_id”: “0x50…sd5adb20”, “_value”: “0x420042” }, “raw”: { “data”: “0x7f…91385”, “topics”: [“0xfd4…b4ead7”, “0x7f…1a91385″] } } Print Page Previous Next Advertisements ”;

Socket.IO – Home

Socket.IO Tutorial PDF Version Quick Guide Resources Job Search Discussion Socket.IO enables real-time bidirectional event-based communication. It works on every platform, browser or device, focusing equally on reliability and speed. Socket.IO is built on top of the WebSockets API (Client side) and Node.js. It is one of the most depended upon library on npm (Node Package Manager). Audience This tutorial has been created for anyone who has a basic knowledge of HTML, Javascript and Node.js work. After completing this tutorial, the reader will be able to build moderately complex real-time websites, back-ends for mobile applications and push notification systems. Prerequisites The reader should have a basic knowledge of HTML, JavaScript and Node.js. If the readers are not acquainted with these, we will suggest them to go through these tutorials first. We will be using Express to ease creating servers; it is not a prerequisite though. Print Page Previous Next Advertisements ”;

Solidity – Pure Functions

Solidity – Pure Functions ”; Previous Next Pure functions ensure that they not read or modify the state. A function can be declared as pure. The following statements if present in the function are considered reading the state and compiler will throw warning in such cases. Reading state variables. Accessing address(this).balance or <address>.balance. Accessing any of the special variable of block, tx, msg (msg.sig and msg.data can be read). Calling any function not marked pure. Using inline assembly that contains certain opcodes. Pure functions can use the revert() and require() functions to revert potential state changes if an error occurs. See the example below using a view function. Example pragma solidity ^0.5.0; contract Test { function getResult() public pure returns(uint product, uint sum){ uint a = 1; uint b = 2; product = a * b; sum = a + b; } } Run the above program using steps provided in Solidity First Application chapter. Output 0: uint256: product 2 1: uint256: sum 3 Print Page Previous Next Advertisements ”;