aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-05-22 12:29:10 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-05-22 12:29:10 -0700
commit82e762fe2f55a0dac7f545bf4494f94db9d72349 (patch)
treea90ba5a2e279b331e6b71503bf48de5ca144ff2f
parenta5acf7bfa0333fc5cb298df2b3ea09eaa56075ac (diff)
downloadbun-82e762fe2f55a0dac7f545bf4494f94db9d72349.tar.gz
bun-82e762fe2f55a0dac7f545bf4494f94db9d72349.tar.zst
bun-82e762fe2f55a0dac7f545bf4494f94db9d72349.zip
[breaking] ServerWebSocket.publish no longer publishes to self by default
This changes `publishToSelf` to be `false` by default instead of `true`. This is a breaking change because it means that `ws.publish("foo", "bar")` will exclude `ws` from the list of websockets to broadcast. We are making this change because many people asked for this and were confused by the status quo - that `w.publish` publishes to self.
-rw-r--r--packages/bun-types/bun.d.ts16
-rw-r--r--src/bun.js/api/server.zig2
-rw-r--r--test/js/bun/websocket/websocket-server.test.ts15
3 files changed, 25 insertions, 8 deletions
diff --git a/packages/bun-types/bun.d.ts b/packages/bun-types/bun.d.ts
index 5558e2db7..d5085e739 100644
--- a/packages/bun-types/bun.d.ts
+++ b/packages/bun-types/bun.d.ts
@@ -1482,16 +1482,18 @@ declare module "bun" {
closeOnBackpressureLimit?: boolean;
/**
- * Control whether or not ws.publish() should include the ServerWebSocket
- * that published the message. This is enabled by default, but it was an API
- * design mistake. A future version of Bun will change this default to
- * `false` and eventually remove this option entirely. The better way to publish to all is to use {@link Server.publish}.
+ * Control whether or not ws.publish() should include the `ServerWebSocket`
+ * that published the message.
*
- * if `true` or `undefined`, {@link ServerWebSocket.publish} will publish to all subscribers, including the websocket publishing the message.
+ * As of Bun v0.6.3, this option defaults to `false`.
*
- * if `false`, {@link ServerWebSocket.publish} will publish to all subscribers excluding the websocket publishing the message.
+ * In Bun v0.6.2 and earlier, this option defaulted to `true`, but it was an API design mistake. A future version of Bun will eventually remove this option entirely. The better way to publish to all is to use {@link Server.publish}.
*
- * @default true
+ * if `true` {@link ServerWebSocket.publish} will publish to all subscribers, including the websocket publishing the message.
+ *
+ * if `false` or `undefined`, {@link ServerWebSocket.publish} will publish to all subscribers excluding the websocket publishing the message.
+ *
+ * @default false
*
*/
publishToSelf?: boolean;
diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig
index 2ce7347c0..463e26c0a 100644
--- a/src/bun.js/api/server.zig
+++ b/src/bun.js/api/server.zig
@@ -2980,7 +2980,7 @@ pub const WebSocketServer = struct {
/// used by publish()
flags: packed struct(u2) {
ssl: bool = false,
- publish_to_self: bool = true,
+ publish_to_self: bool = false,
} = .{},
pub fn fromJS(globalObject: *JSC.JSGlobalObject, object: JSC.JSValue) ?Handler {
diff --git a/test/js/bun/websocket/websocket-server.test.ts b/test/js/bun/websocket/websocket-server.test.ts
index 3868d0267..7259fc8b6 100644
--- a/test/js/bun/websocket/websocket-server.test.ts
+++ b/test/js/bun/websocket/websocket-server.test.ts
@@ -37,6 +37,9 @@ describe("websocket server", () => {
var server = serve({
port: 0,
websocket: {
+ // FIXME: update this test to not rely on publishToSelf: true,
+ publishToSelf: true,
+
open(ws) {
ws.subscribe("all");
},
@@ -115,6 +118,9 @@ describe("websocket server", () => {
var server = serve({
port: 0,
websocket: {
+ // FIXME: update this test to not rely on publishToSelf: true,
+ publishToSelf: true,
+
open(ws) {
ws.subscribe("all");
},
@@ -617,6 +623,9 @@ describe("websocket server", () => {
var server = serve({
port: 0,
websocket: {
+ // FIXME: update this test to not rely on publishToSelf: true,
+ publishToSelf: true,
+
async open(ws) {
// we don't care about the data
// we just want to make sure the DOMJIT call doesn't crash
@@ -647,6 +656,9 @@ describe("websocket server", () => {
var server = serve({
port: 0,
websocket: {
+ // FIXME: update this test to not rely on publishToSelf: true,
+ publishToSelf: true,
+
async open(ws) {
// we don't care about the data
// we just want to make sure the DOMJIT call doesn't crash
@@ -908,6 +920,9 @@ describe("websocket server", () => {
const server = serve({
port: 0,
websocket: {
+ // FIXME: update this test to not rely on publishToSelf: true,
+ publishToSelf: true,
+
open(ws) {
server.stop();
ws.subscribe("test");