Skip to content

Public channels

Public channels are the simplest type of channel in Jetsocket.io. They don’t require authentication and can be subscribed to by any client.

const jetsocket = new Jetsocket("APP_KEY", {
cluster: "APP_CLUSTER",
});
const channel = jetsocket.subscribe("my-channel");
channel.bind("my-event", (data) => {
console.log("Received:", data);
});
const Jetsocket = require("jetsocket");
const jetsocket = new Jetsocket({
appId: "APP_ID",
key: "APP_KEY",
secret: "APP_SECRET",
cluster: "APP_CLUSTER",
});
jetsocket.trigger("my-channel", "my-event", { message: "Hello world!" });

Public channels can have any name that doesn’t start with private- or presence-. Some examples:

// Valid public channel names
jetsocket.subscribe("notifications");
jetsocket.subscribe("live-feed");
jetsocket.subscribe("chat-room");
jetsocket.subscribe("user-123-updates");

You can bind to multiple events on the same channel:

const channel = jetsocket.subscribe("chat-room");
channel.bind("new-message", (data) => {
console.log("New message:", data.message);
});
channel.bind("user-joined", (data) => {
console.log("User joined:", data.username);
});
channel.bind("user-left", (data) => {
console.log("User left:", data.username);
});

You can bind to events across all channels:

jetsocket.bind_global((eventName, data) => {
console.log(`Event ${eventName} received:`, data);
});

You can check the subscription state of a channel:

const channel = jetsocket.subscribe("my-channel");
console.log(channel.subscribed); // true/false
console.log(channel.name); // "my-channel"

Always handle subscription errors:

const channel = jetsocket.subscribe("my-channel");
channel.bind("jetsocket:subscription_error", (error) => {
console.error("Subscription error:", error);
});
jetsocket.connection.bind("error", (error) => {
console.error("Connection error:", error);
});

To unsubscribe from a channel:

jetsocket.unsubscribe("my-channel");

Here’s a complete example of a chat application using public channels:

<!DOCTYPE html>
<html>
<head>
<script src="https://js.jetsocket.io/latest/jetsocket.min.js"></script>
</head>
<body>
<div id="messages"></div>
<input type="text" id="messageInput" placeholder="Type a message...">
<button onclick="sendMessage()">Send</button>
<script>
const jetsocket = new Jetsocket("APP_KEY", {
cluster: "APP_CLUSTER",
});
const channel = jetsocket.subscribe("chat-room");
const messagesDiv = document.getElementById("messages");
channel.bind("new-message", (data) => {
const messageElement = document.createElement("div");
messageElement.textContent = `${data.username}: ${data.message}`;
messagesDiv.appendChild(messageElement);
});
function sendMessage() {
const input = document.getElementById("messageInput");
const message = input.value;
if (message.trim()) {
// Send to server via AJAX or WebSocket
fetch("/send-message", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message, channel: "chat-room" })
});
input.value = "";
}
}
</script>
</body>
</html>

Public channels are perfect for:

  • Live feeds: News updates, social media feeds
  • Public notifications: System announcements, alerts
  • Broadcast messages: Marketing campaigns, announcements
  • Live dashboards: Public metrics, status updates
  • Chat rooms: Public discussions, community chat

Since public channels don’t require authentication:

  • Don’t send sensitive data on public channels
  • Validate all data on the server side
  • Rate limit event triggering to prevent abuse
  • Monitor usage to detect unusual patterns