Documentation

← Back

Quick start.

Select your language:



Prerequisites

An example server in TypeScript

Create a file named quickstart-server.ts with the following contents:

quickstart-server.ts
import { createInterface } from 'readline';

const onReceive = (data: string) => {
  // Messages logged to stdout are sent to the client via the websocket
  console.log("Hello from the server")
  // Messages logged to stderr are not sent
  console.error("Received websocket data:", data);
}

// Messages are received by reading lines from stdin
createInterface({ input: process.stdin })
  .on('line', (line: string) => onReceive(line.trim()));

Start the server by wrapping Node.js in ScaleSocket. Here we use Node.js to run Typescript natively.

terminal
$ scalesocket node -- --experimental-strip-types ./quickstart-server.ts
[INFO] listening at 0.0.0.0:9000

Then connect as many clients as you want to a room at localhost:9000/room-name. For example, using wscat:

terminal
$ npm install -g wscat
added 9 packages in 142ms

$ wscat -c "ws://localhost:9000/example-room"
Connected (press CTRL+C to quit)
> Hello world
< Hello from the server

With default options, anything logged to stdout will be sent to the clients in the room.

The room name, such as example-room can be any URL-compatible string.

Prerequisites

An example server in Python

Create a file named quickstart-server.py with the following contents:

quickstart-server.py
import sys

def on_receive(data: str):
    # Messages logged to stdout are sent to the client via the websocket
    print("Hello from the server")
    # Messages logged to stderr are not sent
    print("Received websocket data:", data, file=sys.stderr)

# Messages are received by reading lines from stdin
for line in sys.stdin:
    on_receive(line.strip())

Start the server by wrapping Python in ScaleSocket:

terminal
$ scalesocket python -- quickstart-server.py
[INFO] listening at 0.0.0.0:9000

Then connect as many clients as you want to a room at localhost:9000/room-name. For example, using wscat:

terminal
$ npm install -g wscat
added 9 packages in 142ms

$ wscat -c "ws://localhost:9000/example-room"
Connected (press CTRL+C to quit)
> Hello world
< Hello from the server

With default options, anything logged to stdout will be sent to the clients in the room.

The room name, such as example-room can be any URL-compatible string.

Prerequisites

An example server in Bash

Create a file named quickstart-server.sh with the following contents:

quickstart-server.sh
#!/usr/bin/env bash

on_receive() {
  local data="$1"
  # Messages logged to stdout are sent to the client via the websocket
  echo "Hello from the server"
  # Messages logged to stderr are not sent
  echo "Received websocket data: $data" >&2
}

# Messages are received by reading lines from stdin
while IFS= read -r line; do
  on_receive "$line"
done

Make the script executable and start the server by wrapping Bash in ScaleSocket:

terminal
$ chmod +x quickstart-server.sh
$ scalesocket bash quickstart-server.sh
[INFO] listening at 0.0.0.0:9000

Then connect as many clients as you want to a room at localhost:9000/room-name. For example, using wscat:

terminal
$ npm install -g wscat
added 9 packages in 142ms

$ wscat -c "ws://localhost:9000/example-room"
Connected (press CTRL+C to quit)
> Hello world
< Hello from the server

With default options, anything logged to stdout will be sent to the clients in the room.

The room name, such as example-room can be any URL-compatible string.

Next.