Sk Shahnawaz-ul Haque
Sk Shahnawaz-ul Haque

Reputation: 588

How to invoke a SignalR Core hub method from Postman/WebSocket King

I have a SignalR Core hub. I can connect to the same hub using clients like Postman or WebSocket King. But, I am unable to invoke a hub method that expects parameters. The same method can be easily invoked from SignalR JS client. I inspected browser developer tools and extracted the message getting transferred from client to server. When I try to invoke the same method from Postman/WebSocket King with the same message it just don't work.

Hub method:

public async Task Subscribe(string[] imeis, string group)
{
   ...     
}

Message getting transferred from SignalR client (accessed from browser developer tools):

{"arguments":[[10001001],"ALL"],"invocationId":"0","streamIds":[],"target":"Subscribe","type":1}

From Postman/WebSocket King, sending this same message does not hit the hub method. Any suggestions?

Upvotes: 4

Views: 4360

Answers (1)

Sk Shahnawaz-ul Haque
Sk Shahnawaz-ul Haque

Reputation: 588

Ok, so for my case, I think I've been able to connect to the server. So basically, it is a 2-step process:

1. Establishing connection

Issue an HTTP POST request to: <SIGNALR_CORE_SERVER_HUB_URL>/negotiate?negotiateVersion=1

The response should be something like this:

HTTP/1.1 200 OK
Content-Length: 256
Content-Type: application/json
Server: Microsoft-IIS/10.0
Access-Control-Allow-Origin: null
Access-Control-Allow-Credentials: true
X-Powered-By: ASP.NET
Date: Tue, 17 Oct 2021 09:08:59 GMT

{"negotiateVersion":1,"connectionId":"TP- 
KX7BpPj4y4rxMFwaznw","connectionToken":"OeGB- 
EKkoYvcSg7Z4p4n0w","availableTransports": 
[{"transport":"WebSockets","transferFormats":["Text","Binary"]}, 
{"transport":"LongPolling","transferFormats":["Text","Binary"]}]}

The connection token value (OeGB-EKkoYvcSg7Z4p4n0w) is important here! Save it for use in Step 2.

2. Invoking SignalR hub method:

Using WebSocket protocol, invoke the URL:<SIGNALR_CORE_SERVER_HUB_URL>?id=OeGB-EKkoYvcSg7Z4p4n0w

After successful connection establishment, pass in the following data to server through websocket:

{"protocol":"json","version":1}

This will result in getting an empty response from server. Following this, invoke the server hub method and pass the arguments as follows:

{"arguments":[["10001001"],"ALL"],"invocationId":"0","streamIds": 
[],"target":"Subscribe","type":1}

Here, "Subscribe" is my SignalR Core Hub method name, which accepts two arguments, an array of strings (["10001001"]) and a string ("ALL").

After this, from server side, push notifications will start coming to client through the same websocket connection.

Upvotes: 4

Related Questions