I have grpc connection between client and server.
At the beginning client send message to server and connection is created.
Server has a background process that after checking something and conditions are true it should send message to client.
How can i do this ?
You can't do anything without the client making a request. I assume what's actually important is that the client doesn't need to know when to make a request, because the server is effectively event-based.
Create a server-streaming method, and call it from the client. The server can provide responses whenever it wants, and the client will then need to read those responses and handle them.
Related
I am working on an C# Asp.NET Core app where clients will connect via a JavaScript WebSocket to the server and the server itself will be the only thing that can send messages to the connected clients.
The clients will not be sending messages back to the server and if they manage to send a message to the server by manually doing so in any browser console, the server will just ignore them.
This is a sample of the C# code I am using to send messages to all connected clients:
foreach(WebSocket webSocket in WebsocketHandler.WebSocketClients.Values)
{
await webSocket.SendAsync(...);
}
Since I am not expecting clients to send messages back to the server (since this is supposed to be a one-way message flow), is a subsequent call to:
await webSocket.ReceiveAsync(...);
immediately after a call to "SendAysnc()" required at all? Are their downsides/consequences to not calling "ReceiveAsync()" after calling "SendAsync()"?
I could not find anything in the documentation from MS that says it is required to call "ReceiveAsync" after calling "SendAsync()" (though most online examples do) and both methods (according to MS documentation) state that "This operation will not block."
My primary concern here with this is whether or not continuously only sending messages and not receiving anything back may either cause a memory leak from many "SendAsync()" calls piling up because it expects some "response" back or the server disconnecting a client(s) because no message is being sent back from the other side of the communication channel and the server thinks the client has dropped.
Regards.
You need to call ReceiveAsync so you can detect when the websocket closes so you can complete the close handshake. Generally when using websockets directly, you'll have 2 async loops running in parallel, the receive loop and send loop (or you can hand off the socket to send like you are doing). You also need to make sure the request that did the websocket upgrade keeps running so that the connection doesn't get closed and in your scenario, you can use the receive loop for that purpose.
Is it possible to send message from one client to another client that is connected to the same server?
I want send data from one client to server Then send to specific client. I think I need to get client ID but I dont know how to get this id and how to send this message to that client from server.
I have a sample here. This is a chat server-client application. Multiple clients can connect to the server. When a client writes a message, the server simply broadcasts it for all clients who are receiving server stream RPC.
https://github.com/cactuaroid/GrpcWpfSample
See these server side implementation. When a client calls Subscribe(), it awaits m_chatService.Added event. When a client calls Write(), it raises the event and event args ChatLog is written on responseStream.
https://github.com/cactuaroid/GrpcWpfSample/blob/f6e8c4b2493c23cdcbaffeca29b5bb6705fbe95c/GrpcWpfSample.Server/Grpc/ChatServiceGrpcServer.cs
https://github.com/cactuaroid/GrpcWpfSample/blob/f6e8c4b2493c23cdcbaffeca29b5bb6705fbe95c/GrpcWpfSample.Server/Model/ChatService.cs
You can add your logic such as specifying channel name to subscribe/write, or define OpenChannel(string password) to be called by client at the first time so that server can bind the client IP address to the channel, whatever as you like.
There's no special gRPC feature that would allow you to do this (all RPC are between a server and a client, there's no "broadcast" or "selective broadcast" feature to reach out to other clients connected to the same server.
The logic you want is something that can definitely be implemented and but details of such solution depend on your need. A naive approach that might work is e.g. this:
each client opens a bidi-streaming call to the server
server keeps a directory of connected clients
once server receives a message from a client, it selects which client it should forward to based on the directory.
server forwards the message to a client.
Needless to say this setup feels a bit complicated (you're basically implementing your own network protocol on top of gRPC), so even though it might be just the right thing for you to do, it would make sense to think about how to simplify the protocol so that you can use features directly supported by gRPC.
I have a server which is connected to a few clients (with WCF and netTcpBinding).
At an undefinite point in time I want the server to call the method of a specific client (and have a string as a parameter). The server only knows which client when he wants to call it.
Basically one client has to wait for a server to send him a message (but in the meantime, other clients call the server as well) and the server needs to know exactly which client he needs to call. (The client has an attribute and the server wants to call the method on the client with a specific attribute)
Is there a way to do this?
EDIT: I thought of a possibility, but I think there is a better way.. for the moment the client will call a method with parameters specifying the ip and port of the client. The server will add it to a list and when the server wants to call the client, he searches the list for the attribute and connects to the client (on the client a service is hosted as long as the client is waiting)
WCF already has support for this built in. You need to create a duplex contract. The server can then call any connected clients whenever it wants.
My client sends messages to the my server using tcp protocol (my server has a listener).
I'm trying to send messages back from the server to the client, but I'm not sure what do I need to add to the client in order to get those messages.
I tried looking it up, but I didn't understand how to implement it.
Could you please assist?
It's the exact same process, but in reverse.
If you intend to receive messages independently (i.e. not in response to a request by the client), you need to make the client a TCP server, too. The client needs to implement exactly the same thing as the server you have now and the server needs to connect to the client.
Can't you use WCF, which supports callback contracts?
If you want a console example of a client server application using a network library see here http://www.networkcomms.net/how-to-create-a-client-server-application-in-minutes/.
If you want a WPF application example then see here http://www.networkcomms.net/creating-a-wpf-chat-client-server-application/
I have a client that sends a request to a server, and the server answer to him.
I want to do this in an asynchronous way. The client sends a request, the server calculates it and returns it to a service in the client. How can i do this?
p.s. let's say the client wants to do add(int a, int b) and the server needs to send the result to a service that run on the client.
A dual binding is only really needed when the server must send information to the client without a request. Since you are making a request, use one of the more basic bindings.
Making the request asynchronous is all about what the client does when it calls the service. One way of doing this is to get the generated client interface to include the event based asynchronous pattern.
When the client calls this method, it simply returns right away. An event is then triggered when the actual response to the request is received. The server itself is completely uninvolved with how the client waits for the response.