OCPP, why WebSockets?
We explained OCPP in our 1st post of the EV and OCPP series, you can read that post for the basics of OCPP. In this post, We will understand Websockets and how it used as a carrier protocol in OCPP.
What is Websockets?
Before going into details of Websockets, lets understand mechanism of HTTP protocol and limitation of same. We know that HTTP is a widely used protocol for client server communications. HTTP is a unidirectional protocol, means client establish connection with server and request for resource always initiated from client end.
When you access a web site over HTTP, browser play role of client and request need to be initiated from the client end, any data from the server will always be against some request from the client. HTTP only unidirectional communication can be a roadblock in the application which needs bidirectional communication. In unidirectional communication, if the server has to send some data to the client, the server has to wait for client to get connected first, which means server keep probing if client connected or not.
Websockets is the answer where bidirectional communication needed. Client can send data to server and similarly server can also send data to client anytime. HTTP and Websockets both works on TCP, and i would prefer to say that Websocket is an upgrade of HTTP
Websocket URL scheme
Websocket resource URL use custom scheme ws and wss. ws for plain text communication and wss for secure (TCP+TLS) communication.
Example,

Here in this example, 5609 port is over Websocket
In Websocket, before any messages can be exchanged, the client and server must negotiate the appropriate parameters to establish the connection. HTTP upgrade negotiate a few parameters such as
Sec-WebSocket-Version – Sent by client to specify version
Sec-WebSocket-Key – Auto generated key by client
Sec-WebSocket-Accept – Server response, signed value of Key
Sec-WebSocket-Protocol – Application sub protocol
Sec-WebSocket-Extensions – Extensions of web sockets
OCPP needs bidirectional protocol, as charger need to send charging session data to CMS, and CMS also need to send commands to charger. CMS can send command to charger such as start transaction , stop transaction etc. Websocket is a bidirectional protocol and also use foundation of HTTP which is widely accepted protocol over the internet and mostly firewall not block HTTP.