SignalR Message Format

12 במרץ 2014

no comments

I looked around for documentation about the message format of Asp.Net SignalR but I could not find any so I had to explore myself and produce the following summary about Asp.Net message format.

Messages are serialized as JSON objects and contain metadata about the handlers and the connection as well as payload (i.e. the data to transfer) The following information is included in the messages:

– Hubs (H): Handlers (i.e methods) on the server as well as on the clients are grouped in "Hubs".
– Method (M): The name of the Handler that will process the message.
– Groups: Clients can be grouped on the servers in "groups" for pub-sub implementation.
– Arguments (A): The actual content to be passed to the hander on the client or on the server.
– Cursor (C): Represents a position in the message stream.
– Index (I): The Id of the handler or Callback.

Hub Message Format

Hub Request format ("send" request)

Data={"I":index,"H":"samplingstreaminghub","M":"ConnectToStream","A":["StreamUri"]}

Hubs messages format (server to client)

{"C":"messageId value", "M":[{"H":"HubName","M":"HandlerName","A": ["argument list as json"]}]}

– {"C":"messageId value", "G": ["groupName"],"g": ["groupName"],"T":1,
    "M":[{"H":"HubName","M":"HandlerName","A": ["argument list as json"]}]}

Hub Payloads format ("send" request or inside "M" element of Hub message)

- {"H":"HubName","M":"HandlerName","A": [argument list as json],                                    "S":{state as json},"I":index}
- {"H":"HubName","M":"HandlerName","A": [argument list as json],"I":index} 
Hub Response examples ("send" response)
- {"I":"0","R":{"return object as json}}
- {"I": 0} -> no result
- {"I": 0, "S":{"x":1},"R":1} –> result
- {"I": 0, "E":"This is an error"} –> error
- {"I": 0, "E":"This is an error", "T": "Some stack trace here"} -> error + stack
The following is a complete list of the messages elements:

Hub Messages:

C – Cursor
M – Messages
T – Timeout (only if true) value is 1
D – Disconnect (only if true) value is 1
R – All Groups (Client groups should be reset to match this list exactly)
G – Groups added
g – Groups removed

Hub payload:

I – Callback Operation index
H – Hub name
M – method name
A – arguments
S – state (if not null)

Hub Method return value:

I – Operation index
R – Result
S – State
E – Error
T – stack trace
D – Error Data

The cursor mechanism
As described above the message contains a cursor for identifying the location in the stream. A cursor represents where a particular client is in an infinite stream of messages. If the client disconnects and then reconnects, it asks the OI streaming API for any messages that arrived after the client’s cursor value. The same thing happens when a connection uses long polling. After a long poll request completes, the client opens a new connection and asks for messages that arrived after the cursor.

To find more information download the signalR source code from github. To find the message format just search for json serialization attributes (by searching for “M” for example). 

Hope this helps

Manu

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*