Events in smart contract is used to notify the client application about an operation or event that has completed. The primary purpose of an Event is to provide callbacks in user interface, which allows us to execute code based on an event. An event comprises of the name of an event and parameters. Parameters are optional. If you are new to Smart Contract, then you can refer to my previous blog post : https://www.jellyfishtechnologies.com/blog/writing-deploying-smart-contracts-blockchain/

Let’s take an example of event in contract :

 


pragma solidity ^0.4.0;
contract Ballot {

struct Voter {
 uint weight;
 bool voted;
 uint8 vote;
 address delegate;
 }
 struct Proposal {
 uint voteCount;
 }
 enum Stage {Init,Reg, Vote, Done}
 Stage public stage = Stage.Init;
 
 address chairperson;
 mapping(address => Voter) voters;
 Proposal[] proposals;

event votingCompleted();
 
 uint startTime;

 modifier validStage(Stage reqStage)
 { require(stage == reqStage);
 _;
 }


 function Ballot(uint8 _numProposals) public {
 chairperson = msg.sender;
 voters[chairperson].weight = 2; // weight is 2 for testing purposes
 proposals.length = _numProposals;
 stage = Stage.Reg;
 startTime = now;
 }

 function register(address toVoter) public validStage(Stage.Reg) {

 if (msg.sender != chairperson || voters[toVoter].voted) return;
 voters[toVoter].weight = 1;
 voters[toVoter].voted = false;
 if (now > (startTime+ 30 seconds)) {stage = Stage.Vote; } 
 }

/// Give a single vote to proposal $(toProposal).
 function vote(uint8 toProposal) public validStage(Stage.Vote) {
 Voter storage sender = voters[msg.sender];
 if (sender.voted || toProposal >= proposals.length) return;
 sender.voted = true;
 sender.vote = toProposal; 
 proposals[toProposal].voteCount += sender.weight;
 if (now > (startTime+ 30 seconds)) {stage = Stage.Done; votingCompleted();} 
 
 }

function winningProposal() public validStage(Stage.Done) constant returns (uint8 _winningProposal) {
 uint256 winningVoteCount = 0;
 for (uint8 prop = 0; prop < proposals.length; prop++)
 if (proposals[prop].voteCount > winningVoteCount) {
 winningVoteCount = proposals[prop].voteCount;
 _winningProposal = prop;
 }
 assert (winningVoteCount > 0);

}
}



You can ignore the non-relevant aspect of the contract and focus only on an event. You can see that an event named “votingCompleted” has been declared in the contract. It is upto the smart contract developer to decide when to fire this event and intimate the listener/front-end application. You can see that the event “votingCompleted” is fired when a certain condition is met i.e. When the ballot stage has reached “Stage.Done” based on the time elapsed.

Our front-end application should listen to the event emitted so as to perform some action based on it.  This is a code written in node.js to listen/subscribe to an event :

 

When the event is emitted by the contract, we receive a callback with the parameters(if specified) and perform whatever action we desire.

 

That was all! If you have any questions/doubts, feel free to comment.