How can I write a WebSocket client in Julia? - websocket

I would like to connect to a WebSocket via Julia. I attempted to get an echo response from wss://echo.websocket.org, but it does not seem to respond as I would have expected it to. Interestingly, it does seem to connect, though, whereas an invalid address will not.
julia> client = connect("echo.websocket.org", 443)
TCPSocket(open, 0 bytes waiting)
julia> println(client, "Hello, world!")
julia> readline(client)
""
Is it possible to accomplish this?

There is now a specific library https://github.com/JuliaWeb/WebSockets.jl. Examples of how to use it are provided in examples/chat.jl and examples/chat-client.html.

Web socket clients cannot be implemented by opening a socket and reading and writing directly to it. There is a reasonably complicated protocol that needs to be implemented. Further, a websocket client is meant to receive push request, and hence needs some way to handle them asynchronously.
There is a websocket client library implemented in Julia: https://github.com/dandeliondeathray/DandelionWebSockets.jl
To install it, do: Pkg.clone("https://github.com/dandeliondeathray/DandelionWebSockets.jl")
To use it involves defining event handlers for network events. Please see here for an example using echo: https://github.com/dandeliondeathray/DandelionWebSockets.jl/blob/b23307f360ef0b62e3064c6b1484599eb660f63f/examples/echo.jl

Related

Problem with gRPC setup. Getting an intermittent RPC unavailable error

I have a grpc server and client that works as expected most of the time, but do get a "transport is closing" error occasionally:
rpc error: code = Unavailable desc = transport is closing
I'm wondering if it's a problem with my setup. The client is pretty basic
connection, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
pb.NewAppClient(connection)
defer connection.Close()
and calls are made with a timeout like
ctx, cancel := context.WithTimeout(ctx, 300*time.Millisecond)
defer cancel()
client.MyGRPCMethod(ctx, params)
One other thing I'm doing is checking the connection to see if it's either open, idle or connecting, and reusing the connection if so. Otherwise, redialing.
Nothing special configuration is happening with the server
grpc.NewServer()
Are there any common mistakes setting up a grpc client/server that I might be making?
After much search, I have finally come to an acceptable and logical solution to this problem.
The root-cause is this: The underlying TCP connection is closed abruptly, but neither the gRPC Client nor Server are 'notified' of this event.
The challenge is at multiple levels:
Kernel's management of TCP sockets
Any intermediary load-balancers/reverse-proxies (by Cloud Providers or otherwise) and how they manage TCP sockets
Your application layer itself and it's networking requirements - whether it can reuse the same connection for future requests not
My solution turned out to be fairly simple:
server = grpc.NewServer(
grpc.KeepaliveParams(keepalive.ServerParameters{
MaxConnectionIdle: 5 * time.Minute, // <--- This fixes it!
}),
)
This ensures that the gRPC server closes the underlying TCP socket gracefully itself before any abrupt kills from the kernel or intermediary servers (AWS and Google Cloud Load Balancers both have larger timeouts than 5 minutes).
The added bonus you will find here is also that any places where you're using multiple connections, any leaks introduced by clients that forget to Close the connection will also not affect your server.
My $0.02: Don't blindly trust any organisation's (even Google's) ability to design and maintain API. This is a classic case of defaults-gone-wrong.
One other thing I'm doing is checking the connection to see if it's either open, idle or connecting, and reusing the connection if so. Otherwise, redialing.
grpc will manage your connections for you, reconnecting when needed, so you should never need to monitor it after creating it unless you have very specific needs.
"transport is closing" has many different reasons for happening; please see the relevant question in our FAQ and let us know if you still have questions: https://github.com/grpc/grpc-go#the-rpc-failed-with-error-code--unavailable-desc--transport-is-closing
I had about the same issue earlier this year . After about 15 minuets I had servers close the connection.
My solution which is working was to create my connection with grpc.Dial once on my main function then create the pb.NewAppClient(connection) on each request. Since the connection was already created latency wasn't an issue. After the request was done I closed the client.

JMeter TCPSampler - how to handle a custom protocol with a periodic keep alive?

I am relatively new to JMeter however I have been doing Performance testing for almost a decade.
I am working with a proprietary TCP protocol that sends a keep alive periodically - through the existing TCP connection.
I am struggling to understand how I can fork the JMeter 'thread group' to handle a TCP Keep alive received over the same TCP session.
Any ideas?
Thank you brainstrust!
edit: I'm using the TCPsampler and have read the help page. I'll try to provide some more detail shortly about what's happening and how the protocol is written.
edit2: Unfortunately because it's a propriety protocol I cannot reveal the exact nature of the protocol itself but it's largely irrelevant to the problem I'm facing.
Basically, I use the 1st TCP sampler to 'start/authenticate' the session with the server. This is configured the following options:
1. TCPClient classname: LengthPrefixedBinaryTCPClientImpl (my protocol is implemented this standard way)
2. Re-use connection ON.
3. Close connection OFF.
4. Set NoDelay OFF.
5. SO_Linger: nothing
6. Text to send: my hex code for the protocol (this is correct)
I get the response from the first TCP request and then I want to start interacting, however in the session, the server sends a keep alive mid-stream, so occassionally when I send a request, I get an unexpected keep alive response instead (it's an open stream of data).
This is what I would like to solve.
I attempted to use a recursive test fragment, so that on KeepAlive response, it would send the request again however one cannot recurse the test fragments (it throws a Java error on Run attempt).
I hope this gives more context! Thank you for your patience (I'm a newbie SO user!)
Please check the below options if it helps with you sceario:-
If "Re-use connection" is selected, connections are shared between
Samplers in the same thread, provided that the exact same host name
string and port are used. Different hosts/port combinations will use
different connections, as will different threads. If both of "Re-use
connection" and "Close connection" are selected, the socket will be
closed after running the sampler. On the next sampler, another socket
will be created. You may want to close a socket at the end of each
thread loop.
If an error is detected - or "Re-use connection" is not selected - the
socket is closed. Another socket will be reopened on the next sample.
The following properties can be used to control its operation:
tcp.status.prefix text that precedes a status numbertcp.status.suffix
text that follows a status numbertcp.status.properties name of
property file to convert status codes to messagestcp.handler Name of
TCP Handler class (default TCPClientImpl) - only used if not specified
on the GUI
For more details:-https://jmeter.apache.org/usermanual/component_reference.html#TCP_Sampler

Erlang websocket client with cowboy server

Im trying to use this websocket client library but with little success.
Erlang websocket client
If someone used this library to build a client talking to a remote server, how were you able to send messages ?
The basic usage shows to call this to inititate a connection,
websocket_client:start_link("wss://echo.websocket.org", ?MODULE, []).
and cast/2 to send a message to a remote server.
websocket_client:cast(self(), {text, <<"message 1">>}).
However, if I try to use the same function else where in the code to send a text/binary frame to remote server, its not helping.
Is there anything that Im missing ?
Thanks!
Keep in mind that the first parameter to websocket_client:cast/2 must be the pid for the websocket_client process. You can get the pid from the start_link call, e.g.:
{ok, Pid} = websocket_client:start_link("wss://echo.websocket.org", ?MODULE, []).
And to cast a message to the remote server:
websocket_client:cast(Pid, {text, <<"message 1">>}).
In the example code for the websocket_client project cast is called from within the init function, in this case they can use self() since the init function is actually executed by the websocket client process.
Similarly if you are calling cast from within your websocket_handle/websocket_info callback functions you can use self() since those are also called by the websocket client process.

Can I send byte by Ruby socket?

generally, we can send byte in C or C++ socket program, so I want to know can i do this in Ruby?
Yes, the Ruby standard library has general support for socket programming; specifically, see the Socket.tcp method for connecting to a host and sending a byte. For example:
require 'socket'
Socket.tcp('127.0.0.1', 9999) do |sock|
sock.send(255.chr, 0) # Send the byte 0xff.
sock.recv(1) # Read a byte from the remote host.
end # Socket is closed upon exiting the block.
If you are looking to do something like. Open a connection, and send some data constructed in bits and bytes via it. You can do that in ruby.
Take a look at this source file from a ruby gem called APNS as a example of how to do it. In the given file, they are sending a push notification(s) through a socket which connects to Apple's APNS.
Ruby has a Socket library, plus Net libraries for higher level communications.
The online version of Programming Ruby has an overview of the network and web libraries.
They all have sample code

Windows Phone 7 > How to abort asynchronous download

I implement asynchronous download to retrieve remote file and store it in IsolatedStorage in order to use it when out of the network.
Everything works great when network is up. However when out of network, I noticed that async donwload may take up to 2 minutes before to fire my MessageBox (which say that connection to server has failed).
Question:
Is there any way to define a timeout ? Let's say that if my application does not receive any answer for X seconds then stop the Async Download and call a method.
Maybe a timeout is not the best pratices. In this case could you give me suggestion ?
I do not want my user wait for 15 seconds max.
PS: my application is suppose to run on wifi only, so I consider that 'network speed' is optimal.
Thx for your help
What I would recommend doing is check the network type first via NetworkInterface. If NetworkInterfaceType is Wireless80211, you have a wireless connection (Wi-Fi). The returned connection can be None in case there is no available way to connect - so you won't even have to start the download if there is no accessible network.
Answering your question, if you are using WebClient, you can't define a timeout. However, you can call instance.CancelAsync(). For a HttpWebRequest you can call instance.Abort().

Resources