Registering for Socket.IO event messages in the .net client can be done several ways:

  1. Using an Action<T> delegate
  2. Using an Action<T> delegate with anonymous methods
  3. Using a lambda expression to an Action<IMessage> delegate instance in the .On(eventName, Action<IMessage>) method (preferred).

By default, all messages received are exposed through the Message event.  Event messages registered via the .On() method will skip the Message event notification.


Event messages are encapsulated within the IMessage interface. 

public interface IMessage
        SocketIOMessageTypes MessageType { get; }
        string RawMessage { get; }

        string Event { get; set; }
        int? AckId { get; }
        string Endpoint { get; }
        string MessageText { get; }
        JsonEncodedEventMessage Json { get; }

        string Encoded { get; }

SocketIOMessageTypes represent the Socket.IO messages types as defined here.

For example, assume the server sends the following event message:

socket.emit('news', { hello: 'world' });

Next, assume the .net client has registered for the ‘news’ event  as such:

socket.On("news", (data) =>
    var raw = data.RawMessage();                            

data (IMessage type) will contain the following: 

(** screen-shot needs to be updated – ‘JsonEncodedMessage’ property is obsolete, please use ‘Json’  - will update doc next rev J. Stott 2/7/11)


Assume you have a C# object that represents this ‘news’ event data payload:

public class RootObject
    public string hello { get; set; }

To directly access this object type, you could use the following in the delegate handler:

socket.On("news", (data) =>
    RootObject roNews = data.Json.GetFirstArgAs<RootObject>();

Last edited Feb 7, 2012 at 4:21 AM by jstott, version 11


No comments yet.