Public channels
Public channels
Section titled “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.
Basic usage
Section titled “Basic usage”Client-side subscription
Section titled “Client-side subscription”const jetsocket = new Jetsocket("APP_KEY", { cluster: "APP_CLUSTER",});
const channel = jetsocket.subscribe("my-channel");
channel.bind("my-event", (data) => { console.log("Received:", data);});Server-side triggering
Section titled “Server-side triggering”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!" });Channel naming
Section titled “Channel naming”Public channels can have any name that doesn’t start with private- or presence-. Some examples:
// Valid public channel namesjetsocket.subscribe("notifications");jetsocket.subscribe("live-feed");jetsocket.subscribe("chat-room");jetsocket.subscribe("user-123-updates");Event binding
Section titled “Event binding”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);});Global event binding
Section titled “Global event binding”You can bind to events across all channels:
jetsocket.bind_global((eventName, data) => { console.log(`Event ${eventName} received:`, data);});Channel state
Section titled “Channel state”You can check the subscription state of a channel:
const channel = jetsocket.subscribe("my-channel");
console.log(channel.subscribed); // true/falseconsole.log(channel.name); // "my-channel"Error handling
Section titled “Error handling”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);});Unsubscribing
Section titled “Unsubscribing”To unsubscribe from a channel:
jetsocket.unsubscribe("my-channel");Complete example
Section titled “Complete example”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>Use cases
Section titled “Use cases”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
Security considerations
Section titled “Security considerations”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
Next steps
Section titled “Next steps”- Learn about private channels for secure communication
- Explore presence channels for user tracking
- Check out authentication for securing your application