neo-project / neo-modules

MIT License
60 stars 100 forks source link

RPC-JSON over WebSockets #847

Closed cschuchardt88 closed 4 months ago

cschuchardt88 commented 10 months ago

Description will be updated soon....

Some things to know

Events and their IDs

public enum WebSocketResponseMessageEvent : byte
{
    Block = 0x02,               // Blockchain.Committed
    MemoryPool = 0x03,          // MemoryPool
    Transaction = 0x05,         // Transaction
    ContractNotify = 0x14,      // ApplicationEngine.Notify
    AppLog = 0x15,              // ApplicationEngine.Log
    DebugLog = 0x16,            // Utility.Log
    Error = 0x19,               // Error(s)
    Method = 0x20,              // Result(s) from a method call
}

Request information

{
    "version": "1.0",       // Protocol version
    "requestid": 999,       // For tracking which response goto which request (-1 for system error)
    "method": "getblock",   // the name of the method to invoke
    "params": [             // parameters for the method
        "0"
    ]
}

config.json

{
  "PluginConfiguration": {
    "Network": 860833102,
    "BindAddress": "127.0.0.1",
    "Port": 10340,
    "SslCertFile": "",
    "SslCertPassword": "",
    "TrustedAuthorities": [],
    "EnableBasicAuthentication": false,
    "User": "",
    "Pass": "",
    "AllowOrigins": [],
    "ConcurrentProxyTimeout": 120,
    "MessageSize": 4096,
    "MaxGasInvoke": 2000000000,
    "DebugMode": false
  }
}

Developers API (all static)

Events

WebSocketServerPlugin.OnRequest(HttpContext httpContext)               // Emits after valid WebSocket connection request is made
WebSocketServerPlugin.OnServerStarted()                                // Emits after WebSocket host is up
WebSocketConnection.OnMessageSent(Guid clientId, JToken json)          // Emits after message is successfully sent to client
WebSocketConnection.OnMessageReceived(Guid clientId, JToken json)      // Emits after a message is received and serialized
WebSocketConnection.OnConnect(Guid clientId)                           // Emits after a successful connection is made
WebSocketConnection.OnDisconnect(Guid clientId)                        // Emits after a successful disconnect

Methods

WebSocketServerPlugin.RegisterMethods(object handler)                                      // Registers methods for a given class with attribute WebSocketMethod
WebSocketServerPlugin.SendAllJson(JToken json, int pluginId)                               // Sends to all clients
WebSocketServerPlugin.SendJson(Guid clientId, int requestId byte eventId, JToken json)     // Send to a specified WebSocket by client id, (note: use "OnConnect" event to get client id)

Properties

WebSocketServerPlugin.HasClients        // Whether the server has valid connections to clients

Creating a method in Code

[WebSocketMethod]
private JToken Echo(JArray @params) // turns into all lowercase for method name
{
    return @params;
}
// -----OR-----
[WebSocketMethod(Name = "echo")] // name is case sensitive
private async Task<JToken> EchoAsync(JArray @params) // method would be "echoasync`, if not specified by "Name" in attribute
{
    return @params;
}

Video of Syncing (Blockchain)

https://github.com/neo-project/neo-modules/assets/8141309/22c5cd49-8e81-4ba9-a11b-5144cbe8b4b9

Screenshots

Block Event (on commit) Screenshot 2023-11-22 184425

Invoke Response (Echo Method) Screenshot 2023-11-22 184405

Contract Response (on syncing/new block) image

Transaction Response (on syncing/new block) image

Block Response (on syncing/new block) image

Change Log

closes #716

roman-khimov commented 10 months ago

I think community will love two absolutely different websocket interfaces.

cschuchardt88 commented 10 months ago

This would make 3? Because neo-cli node has one built-in too (but thats for syncing blockchain). But this is meant to replace #795. Now are you talking about neo-go's? If that is the case maybe we can build the same way in this PR. What my goal is to allow users to have a fully functioning API that does everything that neo-cli and neo-express does. The reason behind this; is to be able to support other platforms and programming languages along with special use cases. The way that neo-core sits right now... it can't be done easily; if not at all. But with the help of this and other people expanding this API. We can tackle web, dotnet and other ecosystems. This also allows others to make plugins to expand the api for custom in house applications that developers or organizations may need for there only business needs. I think the add-on of this new sidechain; it would be time for ALL developers and users to be able to use neo if they choose too. WebSockets would be the easiest way to start. With web3 most people want their own dapp to be able to get blockchain information and full neo-core functionally. Currently you would have to build your own.

cschuchardt88 commented 10 months ago

What this feature offers, is MUST to have. My other reasoning is that with neo3-visual-tracker (vscode-extension) uses 3rd party javascript library (neon-js) for accessing the RPCServer of neo-express. Now this is very buggy as we know. Could be the because of old version. With this websocket API we can easily get new blocks and other information that is needed for this extension. I am currently rebuilding this extension, and need this plugin, or else I'll make it just for neo-express, But i thought the features it offers would be a good choice for community and ecosystem.

cschuchardt88 commented 10 months ago

@roman-khimov @superboyiii We would like to use this feature in neo-express for the updated neo3-visual-tracker to use (vscode extension). Any ETA on or how or if this feature will be added to core?

cschuchardt88 commented 9 months ago

This can wait @shargon for now, im building out the wallet api for it.

cschuchardt88 commented 9 months ago

@superboyiii If this PR can you on your list ASAP for testing. I need this PR to update the UI for neo-express (vscode extension). The extension is pretty bad at the moment. It unworkable. All the project and templates it generates are useless for any new developer; even those that don't know C#.

shargon commented 4 months ago

Repository moved to neo, please re-open there