{"id":22518041,"url":"https://github.com/drylikov/vue-native-websocket","last_synced_at":"2026-04-29T15:33:29.764Z","repository":{"id":262062198,"uuid":"886115277","full_name":"drylikov/vue-native-websocket","owner":"drylikov","description":"Native websocket with vuex integration.","archived":false,"fork":false,"pushed_at":"2024-11-10T08:42:44.000Z","size":108,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"drylikov","last_synced_at":"2025-02-02T03:45:42.853Z","etag":null,"topics":["javascript","vuejs","websocket"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/drylikov.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-10T08:34:19.000Z","updated_at":"2024-11-10T08:42:47.000Z","dependencies_parsed_at":"2024-11-10T09:28:52.731Z","dependency_job_id":"38664700-82b3-4b87-b6b6-91db3bebb1f1","html_url":"https://github.com/drylikov/vue-native-websocket","commit_stats":null,"previous_names":["drylikov/vue-native-websocket"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drylikov%2Fvue-native-websocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drylikov%2Fvue-native-websocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drylikov%2Fvue-native-websocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drylikov%2Fvue-native-websocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/drylikov","download_url":"https://codeload.github.com/drylikov/vue-native-websocket/tar.gz/refs/heads/drylikov","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245957378,"owners_count":20700252,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","vuejs","websocket"],"created_at":"2024-12-07T04:13:57.754Z","updated_at":"2026-04-29T15:33:29.706Z","avatar_url":"https://github.com/drylikov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# vue-native-websocket\n\nnative websocket implementation for Vuejs 2 and Vuex\n\n## Install\n\n``` bash\nyarn add vue-native-websocket\n\n# or\n\nnpm install vue-native-websocket --save\n```\n\n## Usage\n\n#### Configuration\n\nAutomatic socket connection from an URL string\n\n``` js\nimport VueNativeSock from 'vue-native-websocket'\nVue.use(VueNativeSock, 'ws://localhost:9090')\n```\n\nEnable Vuex integration, where `'./store'` is your local apps store:\n\n``` js\nimport store from './store'\nVue.use(VueNativeSock, 'ws://localhost:9090', { store: store })\n```\n\nSet sub-protocol, this is optional option and default is empty string.\n\n``` js\nimport VueNativeSock from 'vue-native-websocket'\nVue.use(VueNativeSock, 'ws://localhost:9090', { protocol: 'my-protocol' })\n```\n\n\nOptionally enable JSON message passing:\n\n``` js\nVue.use(VueNativeSock, 'ws://localhost:9090', { format: 'json' })\n```\n\nJSON message passing with a store:\n\n``` js\nimport store from './store'\nVue.use(VueNativeSock, 'ws://localhost:9090', { store: store, format: 'json' })\n```\n\n#### On Vuejs instance usage\n\n``` js\nvar vm = new Vue({\n  methods: {\n    clickButton: function(val) {\n        // $socket is [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) instance\n        this.$socket.send('some data')\n        // or with {format: 'json'} enabled\n        this.$socket.sendObj({awesome: 'data'})\n    }\n  }\n})\n```\n\n#### Dynamic socket event listeners\n\nCreate a new listener, for example:\n\n``` js\nthis.$options.sockets.onmessage = (data) =\u003e console.log(data)\n```\n\nRemove existing listener\n\n``` js\ndelete this.$options.sockets.onmessage\n```\n\n#### Vuex Store integration\n\nVuex integration works differently depending on if you've enabled a format\n\n##### Without a format enabled\n\nSocket events will commit mutations on the root store corresponding to the following events\n\n`SOCKET_ONOPEN`\n\n`SOCKET_ONCLOSE`\n\n`SOCKET_ONERROR`\n\n`SOCKET_ONMESSAGE`\n\nEach callback is passed the raw websocket event object\n\nUpdate state in the open, close and error callbacks. You can also check the socket state directly with the `this.$socket` object on the main Vue object.\n\nHandle all the data in the `SOCKET_ONMESSAGE` mutation.\n\n``` js\nimport Vue from 'vue'\nimport Vuex from 'vuex'\n\nVue.use(Vuex);\n\nexport default new Vuex.Store({\n  state: {\n    socket: {\n      isConnected: false,\n      message: '',\n    }\n  },\n  mutations:{\n    SOCKET_ONOPEN (state, event)  {\n      state.socket.isConnected = true\n    },\n    SOCKET_ONCLOSE (state, event)  {\n      state.socket.isConnected = false\n    },\n    SOCKET_ONERROR (state, event)  {\n      console.error(state, event)\n    },\n    // default handler called for all methods\n    SOCKET_ONMESSAGE (state, message)  {\n      state.message = message\n    }\n  }\n})\n```\n\n##### With `format: 'json'` enabled\n\nAll data passed through the websocket is expected to be JSON.\n\nEach message is `JSON.parse`d if there is a data (content) response.\n\nIf there is no data, the fallback `SOCKET_ON*` mutation is called with the original event data, as above.\n\nIf there is a `.namespace` on the data, the message is sent to this `namespaced: true` store (be sure to turn this on in the store module).\n\nIf there is a `.mutation` value in the response data, the corresponding mutation is called with the name `SOCKET_[mutation value]`\n\nIf there is an `.action` value in the response data ie. `action: 'customerAdded'`, the corresponding action is called by name:\n\n``` js\nactions: {\n    customerAdded (context) {\n      console.log('action received: customerAdded')     \n    }\n  }\n```\n\nUse the `.sendObj({some: data})` method on the `$socket` object to send stringified json messages.\n\n## Examples\n\nTODO: post your example here!\n\n## Credits\n\nDerived from https://github.com/MetinSeylan/Vue-Socket.io\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrylikov%2Fvue-native-websocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrylikov%2Fvue-native-websocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrylikov%2Fvue-native-websocket/lists"}