Why onmessage never triggers in my BroadcastChannel?

I’m trying to use BroadcastChannel API but it seems that onmessage event is never triggered (or its handler is not working correctly).

const channel = new BroadcastChannel('foobar')

if (channel) {
  channel.onmessage = onMessage
}

export async function onMessage(event: MessageEvent) {
  alert('onMessage') // never triggers
}

export function post(message: Message) {
  alert('post') // works until this point
  channel?.postMessage(message)
}

In other file:

import * as BroadcastChannel from '@/utils/broadcastChannel/core'

BroadcastChannel.post('test')

Am I doing something incorrectly? Am I missing something?