{"id":13846820,"url":"https://github.com/faye/faye-websocket-ruby","last_synced_at":"2025-05-14T09:06:56.316Z","repository":{"id":1898053,"uuid":"2824626","full_name":"faye/faye-websocket-ruby","owner":"faye","description":"Standards-compliant WebSocket client and server","archived":false,"fork":false,"pushed_at":"2023-09-06T21:56:07.000Z","size":435,"stargazers_count":1050,"open_issues_count":7,"forks_count":97,"subscribers_count":27,"default_branch":"main","last_synced_at":"2025-05-14T09:06:17.298Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/faye.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2011-11-22T01:45:25.000Z","updated_at":"2025-04-18T06:00:07.000Z","dependencies_parsed_at":"2024-01-06T22:29:56.051Z","dependency_job_id":"bdc5eee8-d09c-4537-b649-079fe97d2b0c","html_url":"https://github.com/faye/faye-websocket-ruby","commit_stats":{"total_commits":412,"total_committers":22,"mean_commits":"18.727272727272727","dds":0.06310679611650483,"last_synced_commit":"2787792d0b56aea0ae2706d0f1af7082e95ad5e5"},"previous_names":[],"tags_count":43,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faye%2Ffaye-websocket-ruby","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faye%2Ffaye-websocket-ruby/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faye%2Ffaye-websocket-ruby/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faye%2Ffaye-websocket-ruby/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/faye","download_url":"https://codeload.github.com/faye/faye-websocket-ruby/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254110374,"owners_count":22016391,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T18:00:48.362Z","updated_at":"2025-05-14T09:06:56.292Z","avatar_url":"https://github.com/faye.png","language":"Ruby","funding_links":[],"categories":["Ruby","Tools per Language"],"sub_categories":["Ruby"],"readme":"# faye-websocket\n\nThis is a general-purpose WebSocket implementation extracted from the\n[Faye](http://faye.jcoglan.com) project. It provides classes for easily building\nWebSocket servers and clients in Ruby. It does not provide a server itself, but\nrather makes it easy to handle WebSocket connections within an existing\n[Rack](http://rack.github.io/) application. It does not provide any abstraction\nother than the standard [WebSocket\nAPI](https://html.spec.whatwg.org/multipage/comms.html#network).\n\nIt also provides an abstraction for handling\n[EventSource](https://html.spec.whatwg.org/multipage/comms.html#server-sent-events)\nconnections, which are one-way connections that allow the server to push data to\nthe client. They are based on streaming HTTP responses and can be easier to\naccess via proxies than WebSockets.\n\nThe following web servers are supported. Other servers that implement the\n`rack.hijack` API should also work.\n\n- [Goliath](http://postrank-labs.github.com/goliath/)\n- [Phusion Passenger](https://www.phusionpassenger.com/) \u003e= 4.0 with nginx \u003e= 1.4\n- [Puma](http://puma.io/) \u003e= 2.0\n- [Rainbows](http://rainbows.bogomips.org/)\n- [Thin](http://code.macournoyer.com/thin/)\n\n\n## Installation\n\n```\n$ gem install faye-websocket\n```\n\n\n## Handling WebSocket connections in Rack\n\nYou can handle WebSockets on the server side by listening for requests using the\n`Faye::WebSocket.websocket?` method, and creating a new socket for the request.\nThis socket object exposes the usual WebSocket methods for receiving and sending\nmessages. For example this is how you'd implement an echo server:\n\n```ruby\n# app.rb\nrequire 'faye/websocket'\n\nApp = lambda do |env|\n  if Faye::WebSocket.websocket?(env)\n    ws = Faye::WebSocket.new(env)\n\n    ws.on :message do |event|\n      ws.send(event.data)\n    end\n\n    ws.on :close do |event|\n      p [:close, event.code, event.reason]\n      ws = nil\n    end\n\n    # Return async Rack response\n    ws.rack_response\n\n  else\n    # Normal HTTP request\n    [200, { 'Content-Type' =\u003e 'text/plain' }, ['Hello']]\n  end\nend\n```\n\nNote that under certain circumstances (notably a draft-76 client connecting\nthrough an HTTP proxy), the WebSocket handshake will not be complete after you\ncall `Faye::WebSocket.new` because the server will not have received the entire\nhandshake from the client yet. In this case, calls to `ws.send` will buffer the\nmessage in memory until the handshake is complete, at which point any buffered\nmessages will be sent to the client.\n\nIf you need to detect when the WebSocket handshake is complete, you can use the\n`onopen` event.\n\nIf the connection's protocol version supports it, you can call `ws.ping()` to\nsend a ping message and wait for the client's response. This method takes a\nmessage string, and an optional callback that fires when a matching pong message\nis received. It returns `true` if and only if a ping message was sent. If the\nclient does not support ping/pong, this method sends no data and returns\n`false`.\n\n```ruby\nws.ping 'Mic check, one, two' do\n  # fires when pong is received\nend\n```\n\n\n## Using the WebSocket client\n\nThe client supports both the plain-text `ws` protocol and the encrypted `wss`\nprotocol, and has exactly the same interface as a socket you would use in a web\nbrowser. On the wire it identifies itself as `hybi-13`.\n\n```ruby\nrequire 'faye/websocket'\nrequire 'eventmachine'\n\nEM.run {\n  ws = Faye::WebSocket::Client.new('ws://www.example.com/')\n\n  ws.on :open do |event|\n    p [:open]\n    ws.send('Hello, world!')\n  end\n\n  ws.on :message do |event|\n    p [:message, event.data]\n  end\n\n  ws.on :close do |event|\n    p [:close, event.code, event.reason]\n    ws = nil\n  end\n}\n```\n\nThe WebSocket client also lets you inspect the status and headers of the\nhandshake response via its `status` and `headers` methods.\n\nTo connect via a proxy, set the `proxy` option to the HTTP origin of the proxy,\nincluding any authorization information and custom headers you require:\n\n```rb\nws = Faye::WebSocket::Client.new('ws://www.example.com/', [], {\n  :proxy =\u003e {\n    :origin  =\u003e 'http://username:password@proxy.example.com',\n    :headers =\u003e { 'User-Agent' =\u003e 'ruby' }\n  }\n})\n```\n\n\n## Subprotocol negotiation\n\nThe WebSocket protocol allows peers to select and identify the application\nprotocol to use over the connection. On the client side, you can set which\nprotocols the client accepts by passing a list of protocol names when you\nconstruct the socket:\n\n```ruby\nws = Faye::WebSocket::Client.new('ws://www.example.com/', ['irc', 'amqp'])\n```\n\nOn the server side, you can likewise pass in the list of protocols the server\nsupports after the other constructor arguments:\n\n```ruby\nws = Faye::WebSocket.new(env, ['irc', 'amqp'])\n```\n\nIf the client and server agree on a protocol, both the client- and server-side\nsocket objects expose the selected protocol through the `ws.protocol` property.\n\n\n## Protocol extensions\n\nfaye-websocket is based on the\n[websocket-extensions](https://github.com/faye/websocket-extensions-ruby)\nframework that allows extensions to be negotiated via the\n`Sec-WebSocket-Extensions` header. To add extensions to a connection, pass an\narray of extensions to the `:extensions` option. For example, to add\n[permessage-deflate](https://github.com/faye/permessage-deflate-ruby):\n\n```rb\nrequire 'permessage_deflate'\n\nws = Faye::WebSocket.new(env, [], :extensions =\u003e [PermessageDeflate])\n```\n\n\n## Initialization options\n\nBoth the server- and client-side classes allow an options hash to be passed in\nat initialization time, for example:\n\n```ruby\nws = Faye::WebSocket.new(env, protocols, options)\nws = Faye::WebSocket::Client.new(url, protocols, options)\n```\n\n`protocols` as an array of subprotocols as described above, or `nil`. `options`\nis an optional hash containing any of these keys:\n\n- `:extensions` - an array of\n  [websocket-extensions](https://github.com/faye/websocket-extensions-ruby)\n  compatible extensions, as described above\n- `:headers` - a hash containing key-value pairs representing HTTP headers to be\n  sent during the handshake process\n- `:max_length` - the maximum allowed size of incoming message frames, in bytes.\n  The default value is `2^26 - 1`, or 1 byte short of 64 MiB.\n- `:ping` - an integer that sets how often the WebSocket should send ping\n  frames, measured in seconds\n- `:tls` - a hash containing key-value pairs for specifying TLS parameters.\n  These are passed along to EventMachine and you can find\n  [more details here](http://rubydoc.info/gems/eventmachine/EventMachine%2FConnection%3Astart_tls)\n\n### Secure sockets\n\nStarting with version 0.11.0, `Faye::WebSocket::Client` will verify the server\ncertificate for `wss` connections. This is not the default behaviour for\nEventMachine's TLS interface, and so our defaults for the `:tls` option are a\nlittle different.\n\nFirst, `:verify_peer` is enabled by default. Our implementation checks that the\nchain of certificates sent by the server is trusted by your root certificates,\nand that the final certificate's hostname matches the hostname in the request\nURL.\n\nBy default, we use your system's root certificate store by invoking\n`OpenSSL::X509::Store#set_default_paths`. If you want to use a different set of\nroot certificates, you can pass them via the `:root_cert_file` option, which\ntakes a path or an array of paths to the certificates you want to use.\n\n```ruby\nws = Faye::WebSocket::Client.new('wss://example.com/', [], :tls =\u003e {\n  :root_cert_file =\u003e ['path/to/certificate.pem']\n})\n```\n\nIf you want to switch off certificate verification altogether, then set\n`:verify_peer` to `false`.\n\n```ruby\nws = Faye::WebSocket::Client.new('wss://example.com/', [], :tls =\u003e {\n  :verify_peer =\u003e false\n})\n```\n\n## WebSocket API\n\nBoth the server- and client-side `WebSocket` objects support the following API:\n\n- **`on(:open) { |event| }`** fires when the socket connection is established.\n  Event has no attributes.\n- **`on(:message) { |event| }`** fires when the socket receives a message. Event\n  has one attribute, **`data`**, which is either a `String` (for text frames) or\n  an `Array` of unsigned integers, i.e. integers in the range `0..255` (for\n  binary frames).\n- **`on(:error) { |event| }`** fires when there is a protocol error due to bad\n  data sent by the other peer. This event is purely informational, you do not\n  need to implement error recovery.\n- **`on(:close) { |event| }`** fires when either the client or the server closes\n  the connection. Event has two optional attributes, **`code`** and\n  **`reason`**, that expose the status code and message sent by the peer that\n  closed the connection.\n- **`send(message)`** accepts either a `String` or an `Array` of byte-sized\n  integers and sends a text or binary message over the connection to the other\n  peer; binary data must be encoded as an `Array`.\n- **`ping(message, \u0026callback)`** sends a ping frame with an optional message and\n  fires the callback when a matching pong is received.\n- **`close(code, reason)`** closes the connection, sending the given status code\n  and reason text, both of which are optional.\n- **`version`** is a string containing the version of the `WebSocket` protocol\n  the connection is using.\n- **`protocol`** is a string (which may be empty) identifying the subprotocol\n  the socket is using.\n\n\n## Handling EventSource connections in Rack\n\nEventSource connections provide a very similar interface, although because they\nonly allow the server to send data to the client, there is no `onmessage` API.\nEventSource allows the server to push text messages to the client, where each\nmessage has an optional event-type and ID.\n\n```ruby\n# app.rb\nrequire 'faye/websocket'\n\nApp = lambda do |env|\n  if Faye::EventSource.eventsource?(env)\n    es = Faye::EventSource.new(env)\n    p [:open, es.url, es.last_event_id]\n\n    # Periodically send messages\n    loop = EM.add_periodic_timer(1) { es.send('Hello') }\n\n    es.on :close do |event|\n      EM.cancel_timer(loop)\n      es = nil\n    end\n\n    # Return async Rack response\n    es.rack_response\n\n  else\n    # Normal HTTP request\n    [200, { 'Content-Type' =\u003e 'text/plain' }, ['Hello']]\n  end\nend\n```\n\nThe `send` method takes two optional parameters, `:event` and `:id`. The default\nevent-type is `'message'` with no ID. For example, to send a `notification`\nevent with ID `99`:\n\n```ruby\nes.send('Breaking News!', :event =\u003e 'notification', :id =\u003e '99')\n```\n\nThe `EventSource` object exposes the following properties:\n\n- **`url`** is a string containing the URL the client used to create the\n  EventSource.\n- **`last_event_id`** is a string containing the last event ID received by the\n  client. You can use this when the client reconnects after a dropped connection\n  to determine which messages need resending.\n\nWhen you initialize an EventSource with `Faye::EventSource.new`, you can pass\nconfiguration options after the `env` parameter. Available options are:\n\n- **`:headers`** is a hash containing custom headers to be set on the\n  EventSource response.\n- **`:retry`** is a number that tells the client how long (in seconds) it should\n  wait after a dropped connection before attempting to reconnect.\n- **`:ping`** is a number that tells the server how often (in seconds) to send\n  'ping' packets to the client to keep the connection open, to defeat timeouts\n  set by proxies. The client will ignore these messages.\n\nFor example, this creates a connection that allows access from any origin, pings\nevery 15 seconds and is retryable every 10 seconds if the connection is broken:\n\n```ruby\nes = Faye::EventSource.new(es,\n  :headers =\u003e { 'Access-Control-Allow-Origin' =\u003e '*' },\n  :ping    =\u003e 15,\n  :retry   =\u003e 10\n)\n```\n\nYou can send a ping message at any time by calling `es.ping`. Unlike WebSocket\nthe client does not send a response to this; it is merely to send some data over\nthe wire to keep the connection alive.\n\n\n## Running your socket application\n\nThe following describes how to run a WebSocket application using all our\nsupported web servers.\n\n\n### Running the app with Thin\n\nIf you use Thin to serve your application you need to include this line after\nloading `faye/websocket`:\n\n```ruby\nFaye::WebSocket.load_adapter('thin')\n```\n\nThin can be started via the command line if you've set up a `config.ru` file for\nyour application:\n\n```\n$ thin start -R config.ru -p 9292\n```\n\nOr, you can use `rackup`. In development mode, this adds middlewares that don't\nwork with async apps, so you must start it in production mode:\n\n```\n$ rackup config.ru -s thin -E production -p 9292\n```\n\nIt can also be started using the `Rack::Handler` interface common to many Ruby\nservers. You can configure Thin further in a block passed to `run`:\n\n```ruby\nrequire 'eventmachine'\nrequire 'rack'\nrequire 'thin'\nrequire './app'\n\nFaye::WebSocket.load_adapter('thin')\n\nthin = Rack::Handler.get('thin')\n\nthin.run(App, :Port =\u003e 9292) do |server|\n  # You can set options on the server here, for example to set up SSL:\n  server.ssl_options = {\n    :private_key_file =\u003e 'path/to/ssl.key',\n    :cert_chain_file  =\u003e 'path/to/ssl.crt'\n  }\n  server.ssl = true\nend\n```\n\n\n### Running the app with Passenger\n\nfaye-websocket requires either Passenger for Nginx or Passenger Standalone.\n[Apache doesn't work well with WebSockets at this time](https://github.com/phusion/passenger/issues/1202).\nYou do not need any special configuration to make faye-websocket work, it\nshould work out of the box on Passenger provided you use at least Passenger\n4.0.\n\nHowever, you do need to insert the following code in `config.ru` for optimal\nWebSocket performance in Passenger. This is\n[documented in the Passenger manual](https://www.phusionpassenger.com/documentation/Users%20guide%20Nginx.html#tuning_sse_websockets).\n\n```ruby\nif defined?(PhusionPassenger)\n  PhusionPassenger.advertised_concurrency_level = 0\nend\n```\n\nRun your app on Passenger for Nginx by creating a virtual host entry which\npoints to your app's \"public\" directory:\n\n```\nserver {\n  listen 9292;\n  server_name yourdomain.local;\n  root /path-to-your-app/public;\n  passenger_enabled on;\n}\n```\n\nOr run your app on Passenger Standalone:\n\n```\n$ passenger start -p 9292\n```\n\nMore information can be found on [the Passenger\nwebsite](https://www.phusionpassenger.com/support).\n\n\n### Running the app with Puma\n\nPuma has a command line interface for starting your application:\n\n```\n$ puma config.ru -p 9292\n```\n\nOr, you can use `rackup`. In development mode, this adds middlewares that don't\nwork with async apps, so you must start it in production mode:\n\n```\n$ rackup config.ru -s puma -E production -p 9292\n```\n\n\n### Running the app with Rainbows\n\nIf you're using version 4.4 or lower of Rainbows, you need to run it with the\nEventMachine backend and enable the adapter. Put this in your `rainbows.conf`\nfile:\n\n```ruby\nRainbows! { use :EventMachine }\n```\n\nAnd make sure you load the adapter in your application:\n\n```ruby\nFaye::WebSocket.load_adapter('rainbows')\n```\n\nVersion 4.5 of Rainbows does not need this adapter.\n\nYou can run your `config.ru` file from the command line. Again, `Rack::Lint`\nwill complain unless you put the application in production mode.\n\n```\n$ rainbows config.ru -c path/to/rainbows.conf -E production -p 9292\n```\n\n\n### Running the app with Goliath\n\nIf you use Goliath to server your application you need to include this line\nafter loading `faye/websocket`:\n\n```ruby\nFaye::WebSocket.load_adapter('goliath')\n```\n\nGoliath can be made to run arbitrary Rack apps by delegating to them from a\n`Goliath::API` instance. A simple server looks like this:\n\n```ruby\nrequire 'goliath'\nrequire './app'\nFaye::WebSocket.load_adapter('goliath')\n\nclass EchoServer \u003c Goliath::API\n  def response(env)\n    App.call(env)\n  end\nend\n```\n\n`Faye::WebSocket` can also be used inline within a Goliath app:\n\n```ruby\nrequire 'goliath'\nrequire 'faye/websocket'\nFaye::WebSocket.load_adapter('goliath')\n\nclass EchoServer \u003c Goliath::API\n  def response(env)\n    ws = Faye::WebSocket.new(env)\n\n    ws.on :message do |event|\n      ws.send(event.data)\n    end\n\n    ws.rack_response\n  end\nend\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffaye%2Ffaye-websocket-ruby","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffaye%2Ffaye-websocket-ruby","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffaye%2Ffaye-websocket-ruby/lists"}