Socket Mode
An app with no public address, such as a bot on a laptop or behind a home
router, can open the connection itself, as with Slack's Socket Mode. On the
app's card in Apps and integrations, choose Socket Mode under how it
receives, and make an app-level token (xapp-…). The token is shown once,
stored only as a hash, and can be replaced or taken away. It opens connections
and nothing else: every other method refuses it.
- The app calls
apps.connections.openwith the app-level token. A bot token answersnot_allowed_token_type, and an app still on URLssocket_mode_disabled. The answer'surlis a WebSocket address on this server, at/apps/socket, good for one connection within thirty seconds. - Once connected, the app is sent
{"type": "hello", "num_connections": 1, …}. - Events, slash commands, button presses and form submissions arrive as Slack's
envelopes:
envelope_id,type(events_api,slash_commandsorinteractive),payloadandaccepts_response_payload. The payload is what the HTTP request would have carried: theevent_callback, the command's fields as an object, or theblock_actionsorview_submissionpayload. An event's envelope also hasretry_attemptandretry_reason. - The app acknowledges with
{"envelope_id": "…"}. Apayloadbeside it is what its HTTP answer would have been: a command's reply, a press's, or a form'sresponse_action. - With no connection open, or no acknowledgement within four seconds, an event counts as a failed attempt and is retried exactly as for a URL. A command or a press fails as it does for a URL that does not answer, and the person who ran it is told.
- An app may hold 10 connections, and each envelope goes to one of them. A message from the app over 64 KB closes its connection with 1009. The server pings every connection every ten seconds and closes one that stops answering, or that has fallen too far behind reading what it is sent.
- The server sends
{"type": "disconnect", "reason": "link_disabled"}and closes the connection when the token is replaced or taken away, the app goes back to URLs, or the app is deleted or its bot deactivated. It sendsrefresh_requestedwhen it stops. - Commands and event subscriptions of an app on Socket Mode have no URL and no
url_verificationcheck, and its buttons need no interactivity URL. An app goes back to URLs only once none of its commands or subscriptions is without a URL; the refusal names them. - A reverse proxy in front of the server must pass WebSocket upgrades on
/apps/socketas well as on/ws.
Bolt for JavaScript runs unchanged once both of its clients are pointed at this
server. Checked against a local server with @slack/bolt 5.1: the message
event, the command's reply in the channel and the button's rewrite all arrived.
import bolt from "@slack/bolt";
const slackApiUrl = "https://chat.example.com/api/";
const app = new bolt.App({
token: process.env.SLACK_BOT_TOKEN, // xoxb-…
appToken: process.env.SLACK_APP_TOKEN, // xapp-…
socketMode: true,
clientOptions: { slackApiUrl },
// Bolt's Socket Mode client takes its address from here.
installerOptions: { clientOptions: { slackApiUrl } },
});
app.message("ping", async ({ say }) => {
await say({
text: "pong",
blocks: [
{
type: "actions",
elements: [
{ type: "button", action_id: "again", text: { type: "plain_text", text: "Again" } },
],
},
],
});
});
app.command("/hello", async ({ command, ack }) => {
await ack({ response_type: "in_channel", text: `Hello, ${command.text}` });
});
app.action("again", async ({ ack, respond }) => {
await ack();
await respond({ replace_original: true, text: "Pressed again" });
});
await app.start();