Demand response is a promising technology to cope with the ever-increasing peak energy consumption in smart grid. For the smart home energy management system, it may choose to charge the battery when the electricity price is low, and discharge the battery to supply the home load when the electricity price is high. Under this circumstance, the temporally and spatially coupled constraints coexist in the energy demand, battery charging and discharging energy, and energy supply. The coupled constraints make the demand response problem more challenging than the existing ones and thus the existing distributed algorithms cannot be employed to find the optimal solution of the problem. In this paper, we focus on the real-time two-way communications between utility company and multiple residential users, and each user is regarded as a smart home energy management system. A distributed real-time algorithm is proposed to find the optimal energy management scheduling scheme for each user and utility company to maximize the social welfare. To overcome the obstacle brought by the temporally and spatially coupled constraints, dual decomposition technique is employed and the primal problem is decoupled into several independent subproblems which can be solved in a distributed way by each user and utility company without revealing or exchanging their private information. Simulation results are provided to demonstrate that the proposed distributed algorithm can bring potential benefits to the society.