You are looking at historical revision 31613 of this page. It may differ significantly from its current revision.

websockets

Description

websockets is a fast, lightweight, and simple implementation of the websockets protocol. It currently only supports version 13 and no extensions.

websockets provides both a high level and low level API. The high level API provides both a blocking and concurrent interface. Note that contrary to most or even all other websocket implementations it does not provide an asynchronous interface but it does however provide a concurrent backed interface. See the section on the high level interface for more details. The low level API provides all of the primitives for working directly with websocket connections, messages, and fragments. It can be used for special circumstances, like when more fine grained control is desired for accepting or processing fragments, or for building a different high level API. The provided high level API is based on the exposed low level API.

All high level procedures are thread safe. See the low level interface for details on which procedures are and are not thread safe at that level.

Repository

The git repository for the websockets source code is hosted by bitbucket: https://bitbucket.org/thomashintz/websockets.

Requirements

The following eggs are required:

Parameters

current-websocket
[parameter] (current-websocket [websocket])

Only available with with-websocket or with-concurrent-websocket. current-websocket will be bound to a websocket object. Many procedures provide an optional argument for a websocket object and it is bound to current-websocket by default.

ping-interval
[parameter] (ping-interval [number])

How often to ping the client, in seconds, in the background. If 0 then automatic pinging will be disabled. This defaults to 15 seconds.

If this is set to a value greater than 0 then a thread will run in the background sending ping messages to the client at the specified interval. This is used to keep connections open that will otherwise be closed. Often connections without a transmission will be killed after awhile, often after 60s. Receiving pongs in response to a ping will also reset the connection close timer.

pong responses to ping messages are not passed through to the user when using the high level API but they are used to update the timestamp that the connection timeout thread uses to decide if it should kill a connection.

close-timeout
[parameter] (close-timeout [number])

How long to wait, in seconds, for the client to respond to a connection close request before timing out. If 0 then the connection timeout will be disabled. The default value is 5 seconds.

connection-timeout
[parameter] (connection-timeout [number])

The length of time in seconds without a response (of any kind) from the client before the connection to the client will be cut off. If 0 then the connection will never be timed out by the websocket (something else can, of course, timeout causing the websocket connection to fail anyways). The default is 60 seconds.

accept-connection
[parameter] (accept-connection [procedure])

A one-argument (URL path of the current page) procedure which tells whether to accept the websocket connection. If #t accept, #f reject. IT IS HIGHLY RECOMMENDED that this parameter be set otherwise it opens up your application to potential security vulnerabilities. You will probably want to verify it is coming from a specific source. The default is to accept all connections.

access-denied
[parameter] (access-denied [procedure])

A procedure to be called when the origin header value is rejected by the accept-connection procedure.

The default is:

(lambda () (send-status 'forbidden "<h1>Access denied</h1>"))
drop-incoming-pings
[parameter] (drop-incoming-pings [boolean])

Clients should usually not initiate pings so the default is to drop all incoming pings without responding with a pong. This defaults to #t. Set this to #f to respond to incoming pings with a pong.

propagate-common-errors
[parameter] (propagate-common-errors [boolean])

A lot of errors that occur in the lifecycle of a websocket connection are more-or-less expected and it often is not interesting to receive and deal with these errors. The default is to correctly close the connection when an error occurs with the required opcode but the error is then not signaled to the user. The default is #f. Set to #t to receive all errors.

All websocket specific errors are covered by this. The only error you may receive if this is #f is of type websocket and unexpected-error either when things go terribly wrong in the implementation or an error is triggered in user code. <a name="high-level"></a> Note that this parameter is only relevant when using with-websocket or with-concurrent-websocket. If you don't use one of those then all errors will be propagated.

max-frame-size
[parameter] (max-frame-size [number])

The maximum allowed frame payload size. The default is 1MiB. If a frame exceeds this size then its payload will not be read and the connection will be dropped immediately. This signals a message-too-large error.

The maximum frame size supported by websockets is 1GiB.

max-message-size
[parameter] (max-message-size [number])

The maximum allowed total message size. The default is 1MiB. If a frame will cause the max-message-size to be exceeded then its payload is not read and the connection is dropped immediately. This signals a message-too-large error.

The maximum message size supported by websockets is 1GiB.

High level interface

As noted in the description, the high level interface is not a "traditional" asynchronous API as is often seen with other websocket libraries. Instead a blocking and synchronous interface is provided as well as an asynchronous backed interface that looks and behaves like a blocking interface. See the function definitions below for further details.

Note that if you don't use with-websocket or with-concurrent-websocket then you will need to manually handle opening and closing the websocket connection as well as all error and protocol violations. See the low level interface section for more details.

The main difference between with-websocket and with-concurrent-websocket is that messages are guaranteed to be delivered in order with with-websocket. This also means that no messages will be read into memory and processed unless a call is made to receive-message. This includes pong messages that may be sent in response to any pings sent in the background ping thread, if enabled. See the definition of ping-interval for more details on how background pings and pongs work.

with-websocket

[procedure] (with-websocket [procedure])

with-websocket handles the process of setting up and closing off a websocket connection. procedure is a thunk to be executed after the websocket has been successfully setup. When you use with-websocket you can be assured that the connection will always be closed correctly even if errors occur in your application code and for all protocol violations.

with-websocket sends and receives all messages in a blocking fashion. Only one message will be sent or received at a time unless more threads are introduced in procedure. Even then the processing will block sending and receiving whereas it will not with with-concurrent-websocket.

Unless you expect a high volume of messages or messages of very large size on one websocket connection then this is the recommended procedure to use. It is easier to program when you know messages will arrive in order and it is more lightweight.

with-concurrent-websocket

[procedure] (with-concurrent-websocket [procedure])

This will, like with-websocket, handle setting up and closing a websocket connection. procedure is a thunk to be executed after the websocket has been successfully setup.

with-concurrent-websocket adds to with-websocket by running a thread in the background to read in messages as they arrive and spins off a new thread to process each message. Messages can arrive out-of-order, especially if there are a mix of very large and very small messages. with-concurrent-websocket can be very useful if very large messages are expected since they can take a long time to unmask and UTF8 validate.

Sending messages are still done in a blocking fashion but sending is relatively fast and will likely not be a problem. This could change in the future though so don't rely on it.

receive-message

[procedure] (receive-message #!optional (ws (current-websocket)))

Read in a message from the client. Returns two values. The first is the message and the second is the message type, either 'text or 'binary. Regardless of the message type the message itself will always be a string. Takes an optional websocket object that is bound to (current-websocket) by default. It will always block until a message is received but if used within with-concurrent-websocket it will not block other messages from being received or processed.

It is thread safe.

send-message

[procedure] (send-message message-type #!optional (data "") (ws (current-websocket)))

Send a message to the client. message-type is one of the types listed under the message-types section. Usually this will be 'text or 'binary but any valid type is allowed. data is optional and defaults to an empty message. It can be a string or a u8vector that will be sent to the client. Note that currently u8vectors must be copied before being sent due to some CHICKEN internal limitations, strings will not. send-message also takes an optional websocket object that is bound to (current-websocket) by default.

It is thread safe.