Skip to main content

WebSockets

WebSockets keep a live connection open, so both sides can send messages at any time. They are what chats, live dashboards, games and real-time APIs are built on.

The WebSockets subcategory has two halves:

HalfUse it when
Frontend (client)Your bot connects to someone else's server, like a live API or a game server.
Backend (server)Your bot runs its own server that websites and apps connect to.

Each half offers two libraries, and they do not mix:

LibraryWhat it is
WebSocket (ws)The plain, built-in kind. Links start with ws:// or wss://.
Socket.IOAdds named events, rooms and replies on top, but only talks to other Socket.IO apps.

Pick whichever one the other side uses. If you are building both sides yourself, Socket.IO is usually easier.

Names​

Every connection and every server has a name, main by default. All the other blocks take that name, so one bot can hold several connections or run several servers at once: just give each a different name and use it everywhere.

Events​

Both libraries can send events: a name plus some data. Socket.IO has them built in. For plain WebSockets, DisFuse sends events as JSON shaped like { "event": "chat", "data": ... }, and the when ... receives event blocks read that same shape, so a DisFuse client and a DisFuse server understand each other.

Hosting a server​

A server block opens a port on the machine your bot runs on. Your host has to allow that and tell you which port to use. See Hosting.

Client: WebSocket (ws)​

Show the whole flyout

Client: WebSocket (ws)

Connect to any plain WebSocket server (ws:// or wss:// links)

Every connection has a name, "main" by default. Use the same name in all blocks.

Connect​

BlockWhat it does
ws_client_connectOpens a WebSocket connection to a server (the URL starts with ws:// or wss://). The name lets other blocks use this connection. Put "when connection … receives a message" blocks anywhere to react to it.
ws_client_waitOpenPauses until the connection has finished opening, or until the time runs out. Handy right after connecting, before sending something.

Connect with headers, like an API key​

BlockWhat it does
ws_client_connectAdvancedOpens a WebSocket connection and sends extra headers with it, like an Authorization header with an API key. Leave the subprotocol empty unless the API asks for one.

React to the server​

BlockWhat it does
ws_client_onOpenRuns every time the connection opens, including after it reconnects. A good place to log in or subscribe to things.
ws_client_onMessageRuns every time the server sends a message
ws_client_onEventRuns when the server sends JSON like { "event": "chat", "data": ... } with this event name. Use "event data" inside it.
ws_client_onCloseRuns when the connection closes, whether you closed it or the server did
ws_client_onErrorRuns when something goes wrong, like the server being unreachable. Without this block, errors are printed to the console.

What the server sent​

BlockWhat it does
ws_client_messageThe message the server sent, as text
ws_client_messageJsonThe message the server sent, read as JSON. Use the Objects blocks to get keys from it. Empty if the message isn't JSON.
ws_client_eventDataThe "data" part of the event the server sent. It can be text, a number, an object or a list.
ws_client_closeInfoWhy the connection closed. Code 1000 is a normal close, 1006 means it dropped without a goodbye.
ws_client_errorMessageWhat went wrong, as text

Send to the server​

BlockWhat it does
ws_client_sendSends a message to the server. Text is sent as it is; objects and lists are sent as JSON. If the connection is still opening, the message is sent as soon as it opens.

Events are sent as JSON: { "event": "chat", "data": ... }

BlockWhat it does
ws_client_sendEventSends { "event": name, "data": data } as JSON. Use this with servers that expect events in that shape — including DisFuse WebSocket servers.

Connection status​

BlockWhat it does
ws_client_isOpenTrue if the connection is open and ready to send messages
ws_client_stateThe connection's status as text: "connecting", "open", "closing" or "closed"
ws_client_urlThe URL this connection was opened to

Disconnect or reconnect​

BlockWhat it does
ws_client_disconnectCloses the connection. It won't reconnect by itself after this. Close code 1000 means a normal close. Custom codes must be between 3000 and 4999.
ws_client_reconnectCloses the connection (if it's open) and connects again to the same URL, with the same settings

Client: Socket.IO​

Show the whole flyout

Client: Socket.IO

Connect to a server made with Socket.IO (it won't accept plain WebSockets)

Every connection has a name, "main" by default. Use the same name in all blocks.

Connect​

BlockWhat it does
sio_client_connectConnects to a Socket.IO server. The URL usually starts with https:// or http:// — add a path like /chat to the end to join that namespace. The name lets other blocks use this connection.
sio_client_waitConnectedPauses until the connection is ready, or until the time runs out

Connect with login details, headers and more​

BlockWhat it does
sio_client_connectAdvancedConnects to a Socket.IO server with extra settings. Leave anything you don't need empty. Login details are what a DisFuse Socket.IO server reads with "login detail … of this client".

React to the server​

BlockWhat it does
sio_client_onConnectRuns every time the connection is made, including after reconnecting
sio_client_onEventRuns when the server sends an event with this name. Use "event data" inside it.
sio_client_onAnyRuns for every event the server sends, whatever its name. Use "event name" to see which one it was.
sio_client_onDisconnectRuns when the connection is lost or closed
sio_client_onErrorRuns when connecting fails, like when the server is down or rejects the login. Without this block, errors are printed to the console.

What the server sent​

BlockWhat it does
sio_client_eventDataThe data the server sent with the event. It can be text, a number, an object or a list.
sio_client_eventArgsSome servers send more than one value with an event. This is all of them, as a list. "event data" is the first one.
sio_client_eventNameThe name of the event the server sent
sio_client_disconnectReasonWhy it disconnected, like "io server disconnect" (the server kicked it) or "transport close" (the connection dropped)
sio_client_errorMessageWhy it couldn't connect, as text

Answer an event the server is waiting on​

BlockWhat it does
sio_client_replyAnswers the event the server sent, if the server is waiting for an answer. Only the first reply is sent.

Send events to the server​

BlockWhat it does
sio_client_emitSends an event to the server. The data can be text, a number, an object or a list. If it isn't connected yet, it's sent as soon as it connects.
sio_client_emitWithReplySends an event and waits for the server to answer it. Use "reply from the server" inside — it's empty if no answer came in time.
sio_client_replyValueWhat the server answered with. Empty if it didn't answer in time.

Connection status​

BlockWhat it does
sio_client_isConnectedTrue if the connection is open right now
sio_client_idThe ID the server gave this connection. It changes every time it reconnects.

Disconnect or reconnect​

BlockWhat it does
sio_client_disconnectCloses the connection. It won't reconnect by itself after this.
sio_client_reconnectConnects again after the connection was closed with "disconnect"

Run your own server that websites and apps connect to​

Your host must let you open a port for others to reach it

Server: WebSocket (ws)​

Show the whole flyout

Server: WebSocket (ws)

Host a plain WebSocket server. Clients connect to ws://your-host:port

Every server has a name, "main" by default. Use the same name in all blocks.

Start the server​

BlockWhat it does
ws_server_startStarts a WebSocket server that apps and websites can connect to with ws://your-host:port. Most hosts tell you which port you're allowed to use. Put this in "when the bot starts".
ws_server_stopDisconnects every client and stops the server
ws_server_isRunningTrue if the server has been started and not stopped

React to clients​

BlockWhat it does
ws_server_onConnectRuns every time a new client connects to the server
ws_server_onMessageRuns every time any client sends the server a message
ws_server_onEventRuns when a client sends JSON like { "event": "chat", "data": ... } with this event name. Use "event data" inside it.
ws_server_onDisconnectRuns when a client leaves, for any reason. You can still read its ID and data, but can't send to it anymore.

What the client sent​

BlockWhat it does
ws_server_messageThe message the client sent, as text
ws_server_messageJsonThe message the client sent, read as JSON. Use the Objects blocks to get keys from it. Empty if the message isn't JSON.
ws_server_eventDataThe "data" part of the event the client sent. It can be text, a number, an object or a list.
ws_server_closeInfoWhy the client left. Code 1000 or 1001 is a normal close, 1006 means it dropped without a goodbye.

Reply to this client​

BlockWhat it does
ws_server_sendClientSends a message to this client only. Text is sent as it is; objects and lists are sent as JSON.
ws_server_sendEventClientSends { "event": name, "data": data } as JSON to this client only

Send to everyone​

BlockWhat it does
ws_server_broadcastSends a message to everyone connected. "all clients except this one" only works inside a "when a client…" block.
ws_server_broadcastEventSends an event to everyone connected. "all clients except this one" only works inside a "when a client…" block.

Send to one client by ID​

BlockWhat it does
ws_server_sendIdSends a message to one client, found by its ID. Works anywhere.
ws_server_sendEventIdSends an event to one client, found by its ID. Works anywhere.

About this client​

BlockWhat it does
ws_server_clientInfoInformation about this client. The ID is unique and can be saved and used later with "send to client with ID".
ws_server_clientQueryA value from the end of the URL the client connected with. For ws://host:8080/?token=abc, the parameter "token" is "abc".
ws_server_clientHeaderA header the client sent when it connected, like "user-agent" or "authorization"
ws_server_clientIsConnectedTrue if this client hasn't disconnected yet

Remember things about a client​

BlockWhat it does
ws_server_setDataRemembers something about this client for as long as it's connected, like its username after it logs in
ws_server_getDataSomething you saved about this client with "set … of this client's data"
ws_server_getDataByIdSomething you saved about a client, found by its ID. Works anywhere.

Groups (like chat rooms)​

BlockWhat it does
ws_server_joinGroupGroups are like chat rooms: put clients in one, then send a message to everyone in it at once. A client can be in many groups.
ws_server_inGroupTrue if this client has been added to the group
ws_server_sendGroupSends a message to every client in the group. Check "except this client" to skip the one that sent it (only inside a "when a client…" block).
ws_server_sendEventGroupSends an event to every client in the group
ws_server_groupInfoWho is in a group right now
ws_server_forEachInGroupRuns the blocks inside once for every client in the group. Inside, "this client" means the one the loop is on.

All connected clients​

BlockWhat it does
ws_server_clientsInfoEveryone connected to the server right now
ws_server_idConnectedTrue if a client with this ID is connected right now
ws_server_forEachClientRuns the blocks inside once for every connected client. Inside, "this client" means the one the loop is on.

Only let some clients in​

BlockWhat it does
ws_server_onVerifyRuns before a client is let in. Check things like a password in the URL (with "query parameter") and use "reject this connection" to turn them away. Anything saved to the client's data here is kept once they connect.
ws_server_rejectTurns the client away before it connects, and stops the blocks after it. Only letters, numbers and punctuation are sent in the reason.

Disconnect clients​

BlockWhat it does
ws_server_kickCloses this client's connection. Close code 1000 means a normal close. Custom codes must be between 3000 and 4999.
ws_server_kickIdCloses one client's connection, found by its ID. Works anywhere.

Server problems​

BlockWhat it does
ws_server_onStartRuns once the server is up and ready for connections
ws_server_onErrorRuns when the server itself has a problem, like the port already being in use. Without this block, errors are printed to the console.
ws_server_errorMessageWhat went wrong, as text

Server: Socket.IO​

Show the whole flyout

Server: Socket.IO

Host a Socket.IO server. Websites connect with the Socket.IO library

Every server has a name, "main" by default. Use the same name in all blocks.

Start the server​

BlockWhat it does
sio_server_startStarts a Socket.IO server that apps and websites can connect to. "Allow websites" is which websites may connect from a browser: * for any, or a list like https://mysite.com, https://other.com. Put this in "when the bot starts".
sio_server_stopDisconnects every client and stops the server
sio_server_isRunningTrue if the server has been started and not stopped

React to clients​

BlockWhat it does
sio_server_onConnectRuns every time a new client connects to the server
sio_server_onEventRuns when any client sends an event with this name. Use "event data" inside it, and "reply to this event" if the client is waiting for an answer.
sio_server_onAnyRuns for every event any client sends, whatever its name. Use "event name" to see which one it was.
sio_server_onDisconnectRuns when a client leaves, for any reason. You can still read its ID and data here.

What the client sent​

BlockWhat it does
sio_server_eventDataThe data the client sent with the event. It can be text, a number, an object or a list.
sio_server_eventArgsA client can send more than one value with an event. This is all of them, as a list. "event data" is the first one.
sio_server_eventNameWhich event the client sent
sio_server_disconnectReasonLike "client namespace disconnect" (it left on purpose), "transport close" (the connection dropped) or "server namespace disconnect" (you kicked it)

Answer an event the client is waiting on​

BlockWhat it does
sio_server_replyAnswers the event the client sent, if the client is waiting for an answer ("send event … and wait for a reply"). Only the first reply is sent.

Send events to this client​

BlockWhat it does
sio_server_emitClientSends an event to this client only
sio_server_emitClientWithReplySends an event to this client and waits for it to answer. Use "reply from the client" inside — it's empty if no answer came in time.
sio_server_replyValueWhat the client answered with. Empty if it didn't answer in time.

Send to everyone​

BlockWhat it does
sio_server_emitAllSends an event to everyone connected. "all clients except this one" only skips someone inside a "when a client…" block.

Send to one client by ID​

BlockWhat it does
sio_server_emitIdSends an event to one client, found by its ID. Works anywhere.

About this client​

BlockWhat it does
sio_server_clientInfoInformation about this client. The ID can be saved and used later with "send event to client with ID".
sio_server_clientHandshakeSomething the client sent when it connected: its login details (the auth object), a value from its URL, or a header like user-agent
sio_server_clientIsConnectedTrue if this client hasn't disconnected yet

Remember things about a client​

BlockWhat it does
sio_server_setDataRemembers something about this client for as long as it's connected, like its username after it logs in
sio_server_getDataSomething you saved about this client with "set … of this client's data"
sio_server_getDataByIdSomething you saved about a client, found by its ID. Works anywhere.

Rooms (like chat rooms)​

BlockWhat it does
sio_server_joinRoomRooms are like chat rooms: put clients in one, then send an event to everyone in it at once. A client can be in many rooms.
sio_server_joinRoomIdMoves a client in or out of a room, found by its ID. Works anywhere.
sio_server_inRoomTrue if this client is in the room
sio_server_emitRoomSends an event to every client in the room. Check "except this client" to skip the one that sent it (only inside a "when a client…" block).
sio_server_roomInfoWho is in a room right now
sio_server_forEachInRoomRuns the blocks inside once for every client in the room. Inside, "this client" means the one the loop is on.

All connected clients​

BlockWhat it does
sio_server_clientsInfoEveryone connected to the server right now
sio_server_idConnectedTrue if a client with this ID is connected right now
sio_server_forEachClientRuns the blocks inside once for every connected client. Inside, "this client" means the one the loop is on.

Only let some clients in​

BlockWhat it does
sio_server_onAuthRuns before a client is let in. Check its login details and use "reject this connection" to turn it away. You can also save client data or join rooms here.
sio_server_rejectTurns the client away before it connects, and stops the blocks after it. The client sees the reason in its "fails to connect" event.

Disconnect clients​

BlockWhat it does
sio_server_kickCloses this client's connection. It won't reconnect by itself.
sio_server_kickIdCloses the connection of one client (by ID) or of everyone in a room. Works anywhere.

Server started​

BlockWhat it does
sio_server_onStartRuns once the server is up and ready for connections

A worked example: a live Discord feed for a website​

  1. In when the bot starts, start Socket.IO server main on the port your host gives you.
  2. In when a message is received, send event message with the message content as data to all clients on server main.
  3. On your website, connect with the Socket.IO library and listen for message. Every Discord message now shows up live on the page.