Search code examples
websocketcrystal-lang

How to open a connection to a WebSocket server in Crystal (use Crystal as client)


I would like to consume a websocket server connection using Crystal as a client. Does the Crystal standard library provide a WebSocket client library?


Solution

  • Starting with Crystal 0.8.0 you should be able to:

    socket = HTTP::WebSocket.open("example.com", "/connect")
    socket.send "action"
    message = socket.receive
    

    See https://github.com/manastech/crystal/pull/1369 for more details.