API SocketIO

SocketIO

 

Socket.IO is a JavaScript library for real-time web applications. It enables real-time, bi-directional communication between web clients and servers. It has two parts: a client-side library that runs in the browser, and a server-side library for Node.js. Both components have a nearly identical API. Like Node.js, it is event-driven.

 

Messages Types

 

  0: open (Sent from the server when a new transport is opened (recheck))

 

  1: close (Request the close of this transport but does not shut down the connection itself.)

 

  2: ping (Sent by the client. The server should answer with a pong packet containing the same data)

    example

      client sends: 2probe

      server sends: 3probe

 

  3: pong (Sent by the server to respond to ping packets.)

 

  4: string message (actual message, client and server should call their callbacks with the data.)

    example:

      42/chat,[“join”,”{room:1}"]

      4 is the message packet type in the engine.io protocol

      2 is the EVENT type in the socket.io protocol

      /chat is the data which is processed by socket.io

      socket.io will fire the “join” event

      will pass "room: 1" data. It is possible to omit namespace only when it is /.

 

  5: upgrade (Before engine.io switches a transport, it tests, if server and client can communicate over this transport. If this test succeeds, the client sends an upgrade packets which requests the server to flush its cache on the old transport and switch to the new transport.)

 

  6: noop (A noop packet. Used primarily to force a poll cycle when an incoming WebSocket connection is received.)

 

Properties

    API: specifies SocketIO version:

 

ioAPI0: supports socket.io 0.* servers (selected by default)

 

ioAPI1: supports socket.io 1.* servers

 

ioAPI2: supports socket.io 2.* servers

 

ioAPI3: supports socket.io 3.* servers

 

ioAPI4: supports socket.io 4.* servers

 

    Base64: if enabled, binary messages are received as base64.

 

    HandShakeCustomURL: allows customizing URLl to get socket.io session.

 

   HandShakeTimestamp: only enable if you want to send timestamp as a parameter when a new session is requested (enable this property if you try to access a gevent-socketio python server).

 

    HandShakeAuthToken: if the server requires to send a token for authentication, set here the authentication token.

 

    Namespace: allows setting a namespace when connects to the server.

 

    Polling: disabling this property, client will connect directly to server using websocket as transport.

 

    Parameters: allows to set connection parameters.

 

    EncodeParameters: if enabled, parameters are encoded.

 

Methods

Use WriteData method to send messages to socket.io server (following Message Types sections)

1. call method add user and one parameter with John as user name


WriteData("42[\"add user\", \"John\"]");

Events

OnHTTPRequest

Before a new websocket connection is established, socket.io server requires client open a new HTTP connection to get a new session id. In some cases, socket.io server requires authentication using HTTP headers, you can use this event to add custom HTTP headers, like Basic authorization or Bearer token authentication.

OnAfterConnect

This event is called after socket.io connection is successful and client can send messages to server. Here you can subscribe to namespaces for example.